设置button 文字居左,图片居右
2016-11-17 本文已影响0人
京哥
//设置图片居右
-(void)setImageToRight
{
NSDictionary *attribute = @{NSFontAttributeName:btn.titleLabel.font};
//获取文本的宽度
CGFloat btnWidth = [btn.titleLabel.text boundingRectWithSize:CGSizeMake(0, 24)
options:\
NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size.width;
//通过调节文本和图片的内边距到达目的
btn.imageEdgeInsets = UIEdgeInsetsMake(0, btnWidth, 0, -btnWidth);
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn.imageView.image.size.width, 0, btn.imageView.image.size.width)];
}