国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

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

2019-10-21 18:54:28
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了iOS中UIButton的三大UIEdgeInsets屬性用法,分別講解了contentEdgeInsets、imageEdgeInsets和titleEdgeInsets三個(gè)屬性在創(chuàng)建UIButton時(shí)對(duì)樣式的控制,需要的朋友可以參考下
 

UIEdgeInsets是什么

UIEdgeInsets是什么?我們點(diǎn)進(jìn)去看一下:

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

哪三個(gè)UIEdgeInsets屬性

不知道大家發(fā)現(xiàn)沒(méi)有,UIButton里面有三個(gè)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后面有個(gè)UI_APPEARANCE_SELECTOR是什么意思呢?

提示:UI_APPEARANCE_SELECTOR標(biāo)記的屬性都支持通過(guò)外觀代理來(lái)定制。
舉例,設(shè)置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)建一個(gè)button,讓button的title居左,以便觀察:

iOS,UIButton,UIEdgeInsets

UIButton的contentEdgeInsets屬性

@property(nonatomic)     UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero

contentEdgeInsets里有一個(gè)content應(yīng)該指的就是UIButton的title。

參數(shù)含義:

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

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

left = 10; //代表以當(dāng)前位置為基準(zhǔn),向右移動(dòng)10個(gè)像素left = -10; //代表以當(dāng)前位置為基準(zhǔn),向左移動(dòng)10個(gè)像素

向右移動(dòng)20個(gè)像素

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

向右移動(dòng)20個(gè)像素,left = 20,就可以了。

iOS,UIButton,UIEdgeInsets

向左移動(dòng)20個(gè)像素

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

iOS,UIButton,UIEdgeInsets

UIButton的titleEdgeInsets屬性

titleEdgeInsets和contentEdgeInsets的作用差不多。我們及設(shè)置contentEdgeInsets,又設(shè)置titleEdgeInsets,會(huì)怎樣呢?

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

看一下效果:

iOS,UIButton,UIEdgeInsets

UIButton的imageEdgeInsets屬性

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

運(yùn)行一下:

iOS,UIButton,UIEdgeInsets

向右移動(dòng)50個(gè)像素

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

看看效果:

iOS,UIButton,UIEdgeInsets

向左移動(dòng)50個(gè)像素

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

看看效果:

iOS,UIButton,UIEdgeInsets

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



注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 綦江县| 应城市| 鄂伦春自治旗| 五华县| 海宁市| 永城市| 达拉特旗| 大港区| 宁远县| 公安县| 米脂县| 澎湖县| 兴仁县| 广西| 乌什县| 平远县| 焉耆| 慈利县| 息烽县| 沭阳县| 陆丰市| 屏边| 宁远县| 怀来县| 乌拉特中旗| 怀化市| 中西区| 堆龙德庆县| 繁峙县| 精河县| 射洪县| 焉耆| 黄冈市| 钟山县| 杭锦后旗| 东乡县| 宝鸡市| 阿图什市| 麟游县| 禹城市| 灵石县|