最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解iOS中UIButton的三大UIEdgeInsets屬性用法

 更新時間:2016年04月25日 14:38:23   作者:李剛  
這篇文章主要介紹了iOS中UIButton的三大UIEdgeInsets屬性用法,分別講解了contentEdgeInsets、imageEdgeInsets和titleEdgeInsets三個屬性在創(chuàng)建UIButton時對樣式的控制,需要的朋友可以參考下

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是個結(jié)構(gòu)體類型。里面有四個參數(shù),分別是:top, left, bottom, right。這四個參數(shù)表示距離上邊界、左邊界、下邊界、右邊界的距離。

哪三個UIEdgeInsets屬性

不知道大家發(fā)現(xiàn)沒有,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屬性,可以直接調(diào)用:

[[UIButton appearance] setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

創(chuàng)建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];

創(chuàng)建一個button,讓button的title居左,以便觀察:

2016425143210943.png (146×97)

UIButton的contentEdgeInsets屬性

@property(nonatomic)     UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; 

// default is UIEdgeInsetsZero

contentEdgeInsets里有一個content應該指的就是UIButton的title。

參數(shù)含義:

上面我們講了UIEdgeInsets是個結(jié)構(gòu)體類型。里面有四個參數(shù),分別是:top, left, bottom, right。這四個參數(shù)表示距離上邊界、左邊界、下邊界、右邊界的距離。

這四個參數(shù)的值可以為正值,也可以為負值。拿left舉例:

left = 10; //代表以當前位置為基準,向右移動10個像素
left = -10; //代表以當前位置為基準,向左移動10個像素

向右移動20個像素

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

向右移動20個像素,left = 20,就可以了。

2016425143256442.png (154×80)

向左移動20個像素

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

2016425143338999.png (146×80)

UIButton的titleEdgeInsets屬性

titleEdgeInsets和contentEdgeInsets的作用差不多。我們及設置contentEdgeInsets,又設置titleEdgeInsets,會怎樣呢?

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

看一下效果:

2016425143400935.png (152×97)

UIButton的imageEdgeInsets屬性

創(chuàng)建一個帶照片的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];

運行一下:

2016425143505909.png (169×161)

向右移動50個像素

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

看看效果:

2016425143526865.png (170×190)

向左移動50個像素

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

看看效果:

2016425143547866.png (181×178)

大家可以自行設置其他三個參數(shù)看看效果是怎樣的,自己動手便于理解。

相關(guān)文章

最新評論

丽水市| 临桂县| 紫云| 灵川县| 富宁县| 定边县| 张家港市| 南汇区| 利辛县| 黔南| 博罗县| 绍兴县| 航空| 涪陵区| 吉林省| 东方市| 陵水| 六枝特区| 浦城县| 咸阳市| 扶绥县| 探索| 锡林浩特市| 富裕县| 蓬莱市| 华容县| 普格县| 盈江县| 临汾市| 霍林郭勒市| 尤溪县| 泸定县| 贡嘎县| 正安县| 西充县| 水富县| 藁城市| 广西| 牟定县| 灵川县| 修水县|