源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

详解iOS中UIButton的三大UIEdgeInsets属性用法

  • 时间:2020-01-06 08:50 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:详解iOS中UIButton的三大UIEdgeInsets属性用法
[b]UIEdgeInsets是什么[/b] 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;
UIEdgeInsets是个结构体类型。里面有四个参数,分别是:top, left, bottom, right。这四个参数表示距离上边界、左边界、下边界、右边界的距离。 [b]哪三个UIEdgeInsets属性[/b] 不知道大家发现没有,UIButton里面有三个UIEdgeInsets属性,分别是:
@property(nonatomic)     UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero

@property(nonatomic)     UIEdgeInsets titleEdgeInsets;        // default is UIEdgeInsetsZero

@property(nonatomic)     UIEdgeInsets imageEdgeInsets;        // default is UIEdgeInsetsZero

contentEdgeInsets后面有个UI_APPEARANCE_SELECTOR是什么意思呢? 提示:UI_APPEARANCE_SELECTOR标记的属性都支持通过外观代理来定制。 举例,设置UIButton的contentEdgeInsets属性,可以直接调用:
[[UIButton appearance] setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

创建UIButton:
UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake(50, 200, 200, 50);
[button setTitle:@"我是UIButton" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
button.titleLabel.textAlignment = NSTextAlignmentLeft;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.view addSubview:button];
创建一个button,让button的title居左,以便观察: [img]http://files.jb51.net/file_images/article/201604/2016425143210943.png?2016325143217[/img] [b]UIButton的contentEdgeInsets属性[/b]
@property(nonatomic)     UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; 

// default is UIEdgeInsetsZero

contentEdgeInsets里有一个content应该指的就是UIButton的title。 参数含义: 上面我们讲了UIEdgeInsets是个结构体类型。里面有四个参数,分别是:top, left, bottom, right。这四个参数表示距离上边界、左边界、下边界、右边界的距离。 这四个参数的值可以为正值,也可以为负值。拿left举例:
left = 10; //代表以当前位置为基准,向右移动10个像素
left = -10; //代表以当前位置为基准,向左移动10个像素
向右移动20个像素
button.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
向右移动20个像素,left = 20,就可以了。 [img]http://files.jb51.net/file_images/article/201604/2016425143256442.png?201632514334[/img] 向左移动20个像素
button.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
[img]http://files.jb51.net/file_images/article/201604/2016425143338999.png?2016325143346[/img] [b]UIButton的titleEdgeInsets属性[/b] titleEdgeInsets和contentEdgeInsets的作用差不多。我们及设置contentEdgeInsets,又设置titleEdgeInsets,会怎样呢?
button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
button.contentEdgeInsets = UIEdgeInsetsMake(0, 20 , 0, 0);
看一下效果: [img]http://files.jb51.net/file_images/article/201604/2016425143400935.png?201632514347[/img] [b]UIButton的imageEdgeInsets属性[/b] 创建一个带照片的button:
UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake(50, 200, 200, 200);
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
[button setImage:[UIImage imageNamed:@"test"] forState:UIControlStateNormal];
[self.view addSubview:button];
运行一下: [img]http://files.jb51.net/file_images/article/201604/2016425143505909.png?2016325143513[/img] 向右移动50个像素
button.imageEdgeInsets = UIEdgeInsetsMake(0, 50, 0, 0);
看看效果: [img]http://files.jb51.net/file_images/article/201604/2016425143526865.png?2016325143533[/img] 向左移动50个像素
button.imageEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0);
看看效果: [img]http://files.jb51.net/file_images/article/201604/2016425143547866.png?2016325143554[/img] 大家可以自行设置其他三个参数看看效果是怎样的,自己动手便于理解。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部