概述
首先要限制图标的大小,否则可能因为图标太大,会把文件撑出按钮区域,从而完全看不到文字,这点没找到UIButton相关配置方法。我们可以修改原图,或者直接在代码里面调整图标的大小,下面是一个调整图标大小的方法- (UIImage*)adjustImage:(UIImage *)inImage width:(CGFloat)width
height:(CGFloat)height {
CGFloat destW = width;
CGFloat destH = height;
CGFloat sourceW = width;
CGFloat sourceH = height;
CGImageRef imageRef = inImage.CGImage;
CGContextRef bitmap = CGBitmapContextCreate(NULL,
destW,
destH,
CGImageGetBitsPerComponent(imageRef),
4*destW,
CGImageGetColorSpace(imageRef), kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(bitmap, CGRectMake(0, 0, sourceW, sourceH), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
然后按钮的配置和网上的一样,这个已经写烂了,我就不多解释,下面提供一段我测试成功的代码片段UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(32, 10, 80, 60)];
[btn setTitle:@"触屏拍摄" forState:UIControlStateNormal];
UIImage *img = [UIImage imageNamed:@"touch.png"];
img = [self adjustImage:img width: 32 height:32];
[btn setImage:img forState:UIControlStateNormal];//button的填充图片
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor redColor];
btn.imageEdgeInsets = UIEdgeInsetsMake(- (btn.frame.size.height - btn.titleLabel.frame.size.height- btn.titleLabel.frame.origin.y),(btn.frame.size.width -btn.titleLabel.frame.size.width)/2.0f -btn.imageView.frame.size.width, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(btn.frame.size.height-btn.imageView.frame.size.height-btn.imageView.frame.origin.y, -btn.imageView.frame.size.width, 0, 0);
最后
以上就是神勇煎饼为你收集整理的button html ios,iOS实现UIButton图标和文字上下布局的全部内容,希望文章能够帮你解决button html ios,iOS实现UIButton图标和文字上下布局所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复