ImageEdgeInsets/TitleEdgeInsets的用法

UIEdgeInsets类型

typedef struct UIEdgeInsets { CGFloat top, left, bottom, right; }UIEdgeInsets;

在masonry写视图布局的时候,很容易理解,结构体UIEdgeInsets,约束的是子试图分别距离父视图的 上/左/下/右的 内边距

比如:

[_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.mas_offset(UIEdgeInsetsMake(10, 10, 10, 10));
}];

UIButton的ImageEdgeInsets/TitleEdgeInsets

ImageEdgeInsets: 在UIButton内,image左右平移,上下平移
TitleEdgeInsets: 在UIButton内,image左右平移,上下平移
self.bounds = CGRectMake(0, 0, 40, 40);
[self setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, - 10)];
[self setTitleEdgeInsets:UIEdgeInsetsMake(0, - 20, 0, 20)];
注意:

*1. top/bottom,left/right一定要成对出现
*2.titleLable和imageView的宽度总和大于button的宽度,ImageEdgeInsets设置会有误

总结

CGSize imageSize = self.imageView.intrinsicContentSize;
CGSize titleSize = self.titleLabel.intrinsicContentSize;

图片在右,文字在左

[self setImageEdgeInsets:UIEdgeInsetsMake(0, titleSize.width, 0, - titleSize.width)];
[self setTitleEdgeInsets:UIEdgeInsetsMake(0, - imageSize.width, 0, imageSize.width)];

图片在上,文字在下

[self setImageEdgeInsets:UIEdgeInsetsMake(- titleSize.height / 2 , titleSize.width / 2, titleSize.height / 2, titleSize.width / 2)];
[self setTitleEdgeInsets:UIEdgeInsetsMake(imageSize.height / 2, - imageSize.width / 2, - imageSize.height / 2, imageSize.width / 2)];

图片在下,文字在上

[self setImageEdgeInsets:UIEdgeInsetsMake(titleSize.height / 2, titleSize.width / 2, - titleSize.height / 2, titleSize.width / 2)];
[self setTitleEdgeInsets:UIEdgeInsetsMake(- imageSize.height / 2, -imageSize.width / 2, imageSize.height / 2, imageSize.width / 2)];

你可能感兴趣的:(ImageEdgeInsets/TitleEdgeInsets的用法)