UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 44)];
[btn setBackgroundColor:[UIColor grayColor]];
[btn setImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];
[btn setTitle:@"按钮" forState:UIControlStateNormal];
正常设置图片 和文字的效果是
1.png
需要设置图片和文字整体靠左
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//居左显示
效果如下:
2.png
如果觉得靠的太紧用
setImageEdgeInsets
setTitleEdgeInsets
即可
contentHorizontalAlignment 是UIControl的属性
除了 UIControlContentHorizontalAlignment,
还有 UIControlContentVerticalAlignment
举一个例子看看:
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
3.png
网友评论