UIEdgeInsets
typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
} UIEdgeInsets;
在UIButton中有三个对EdgeInsets的设置:ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets
@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero
@property(nonatomic) UIEdgeInsets titleEdgeInsets; // default is UIEdgeInsetsZero
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted; // default is NO. if YES, shadow reverses to shift between engrave and emboss appearance
@property(nonatomic) UIEdgeInsets imageEdgeInsets; // default is UIEdgeInsetsZero
UIEdgeInsetsMake
里面的四个参数表示距离上边界、左边界、下边界、右边界的距离,默认都为零,title/image在button的正中央
UIKIT_STATIC_INLINE UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) {
UIEdgeInsets insets = {top, left, bottom, right};
return insets;
}
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.frame = CGRectMake(50, 50, 200, 60);
[button addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
[button setTitle:@"点击我啊" forState:(UIControlStateNormal)];
[button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
button.backgroundColor = [UIColor blackColor];
[button setImage:[UIImage imageNamed:@"icon_pass"] forState:(UIControlStateNormal)];
// 默认的是图片在左title 在右
// 1,图片在右边,title 在左边 可以调左边距 也可以调右边距
// button.imageEdgeInsets = UIEdgeInsetsMake(0,0,0,-150);// 和(0,150,0,0) 一样效果
// button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0,50); // (0,-50,0,0) 一样效果
// 2 图片在上 title 在下面 // 具体数字可以自己调
button.imageEdgeInsets = UIEdgeInsetsMake(- 20,50,0,0);
button.titleEdgeInsets = UIEdgeInsetsMake(32, 0, 0,0);
[self.view addSubview:button];