详解详解iOS中中UIButton的三大的三大UIEdgeInsets属性用法属性用法
UIEdgeInsets是什么是什么
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。这四个参数表示距离上边界、左边界、下
边界、右边界的距离。
哪三个哪三个UIEdgeInsets属性属性
不知道大家发现没有,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);
;
forState:UIControlStateNormal];
];
button.titleLabel.textAlignment = NSTextAlignmentLeft;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.view addSubview:button];
创建一个button,让button的title居左,以便观察:
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个像素
评论0
最新资源