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

时间:2022-06-08 14:54:40

uiedgeinsets是什么

uiedgeinsets是什么?我们点进去看一下:

?
1
2
3
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属性,分别是:

?
1
2
3
4
5
@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属性,可以直接调用:

?
1
[[uibutton appearance] settitleedgeinsets:uiedgeinsetsmake(0, 0, 0, 0)];

创建uibutton:

?
1
2
3
4
5
6
7
8
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居左,以便观察:

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

uibutton的contentedgeinsets属性

?
1
2
3
@property(nonatomic)     uiedgeinsets contentedgeinsets ui_appearance_selector;
 
// default is uiedgeinsetszero

contentedgeinsets里有一个content应该指的就是uibutton的title。

参数含义:

上面我们讲了uiedgeinsets是个结构体类型。里面有四个参数,分别是:top, left, bottom, right。这四个参数表示距离上边界、左边界、下边界、右边界的距离。

这四个参数的值可以为正值,也可以为负值。拿left举例:

?
1
2
left = 10; //代表以当前位置为基准,向右移动10个像素
left = -10; //代表以当前位置为基准,向左移动10个像素

向右移动20个像素

?
1
button.contentedgeinsets = uiedgeinsetsmake(0, 20, 0, 0);

向右移动20个像素,left = 20,就可以了。

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

向左移动20个像素

?
1
button.contentedgeinsets = uiedgeinsetsmake(0, -20, 0, 0);

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

uibutton的titleedgeinsets属性

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

?
1
2
button.titleedgeinsets = uiedgeinsetsmake(0, 20, 0, 0);
button.contentedgeinsets = uiedgeinsetsmake(0, 20 , 0, 0);

看一下效果:

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

uibutton的imageedgeinsets属性

创建一个带照片的button:

?
1
2
3
4
5
6
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];

运行一下:

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

向右移动50个像素

?
1
button.imageedgeinsets = uiedgeinsetsmake(0, 50, 0, 0);

看看效果:

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

向左移动50个像素

?
1
button.imageedgeinsets = uiedgeinsetsmake(0, -50, 0, 0);

看看效果:

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

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