开发过程中经常遇到通过imageEdgeInsets和titleEdgeInsets去设置button中的image,title的位置,来达到想要的效果。但因为对其原理的不了解,经常碰壁,设置了却达不到自己想要的效果。终于找到这篇文章,文章作者解析的很详细,这里记录一下,方便自己日后查阅。
1.常用的button样式,图片在上,文字在下
UIButton *btn = [UIButton new];
CGFloat imageWidth = btn.imageView.intrinsicContentSize.width;
CGFloat imageHeight = btn.imageView.intrinsicContentSize.height;
CGFloat labelHeight = btn.titleLabel.intrinsicContentSize.height;
btn.imageEdgeInsets = UIEdgeInsetsMake(0, (btnwidth - imageWidth) / 2, labelHeight, (btnwidth - imageWidth) / 2);
btn.titleEdgeInsets = UIEdgeInsetsMake(imageHeight+10, -imageWidth, 0, 0);
转载:button的imageEdgeInsets和titleEdgeInsets原理
demo地址: SPButton