UIButton中的三个UIEdgeInsets属性(二)

接着昨天的UIButton中的三个UIEdgeInsets属性,今天我们具体谈谈UIButton的contentEdgeInsets、titleEdgeInsets、imageEdgeInsets属性。

创建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居左,以便观察:

1

UIButton的contentEdgeInsets属性

@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,就可以了。

2

向左移动20个像素

button.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);

2

UIButton的titleEdgeInsets属性

titleEdgeInsets和contentEdgeInsets的作用差不多。我们及设置contentEdgeInsets,又设置titleEdgeInsets,会怎样呢?

button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
button.contentEdgeInsets = UIEdgeInsetsMake(0, 20 , 0, 0);

看一下效果:

3

UIButton的titleEdgeInsets属性

创建一个带照片的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];

运行一下:

5

向右移动50个像素

button.imageEdgeInsets = UIEdgeInsetsMake(0, 50, 0, 0);

看看效果:

6

向左移动50个像素

button.imageEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0);

看看效果:

7

大家可以自行设置其他三个参数看看效果是怎样的,自己动手便于理解。

ios

 Jul 4th2015 2:14 pm  ios开发 
来源:刚刚在线(微信:iOSDevTip),欢迎分享本文,转载请保留出处! 
原文链接:http://www.superqq.com/blog/2015/07/04/uibuttonzhong-de-san-ge-uiedgeinsetsshu-xing-(er-)/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值