詳解iOS中UIButton的三大UIEdgeInsets屬性用法
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是個結(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標(biāo)記的屬性都支持通過外觀代理來定制。
舉例,設(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)建一個button,讓button的title居左,以便觀察:

UIButton的contentEdgeInsets屬性
@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero
contentEdgeInsets里有一個content應(yīng)該指的就是UIButton的title。
參數(shù)含義:
上面我們講了UIEdgeInsets是個結(jié)構(gòu)體類型。里面有四個參數(shù),分別是:top, left, bottom, right。這四個參數(shù)表示距離上邊界、左邊界、下邊界、右邊界的距離。
這四個參數(shù)的值可以為正值,也可以為負(fù)值。拿left舉例:
left = 10; //代表以當(dāng)前位置為基準(zhǔn),向右移動10個像素 left = -10; //代表以當(dāng)前位置為基準(zhǔn),向左移動10個像素
向右移動20個像素
button.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
向右移動20個像素,left = 20,就可以了。

向左移動20個像素
button.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);

UIButton的titleEdgeInsets屬性
titleEdgeInsets和contentEdgeInsets的作用差不多。我們及設(shè)置contentEdgeInsets,又設(shè)置titleEdgeInsets,會怎樣呢?
button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0); button.contentEdgeInsets = UIEdgeInsetsMake(0, 20 , 0, 0);
看一下效果:

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];
運(yùn)行一下:

向右移動50個像素
button.imageEdgeInsets = UIEdgeInsetsMake(0, 50, 0, 0);
看看效果:

向左移動50個像素
button.imageEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0);
看看效果:

大家可以自行設(shè)置其他三個參數(shù)看看效果是怎樣的,自己動手便于理解。
- iOS UIButton 點(diǎn)擊無響應(yīng)的解決辦法
- iOS如何將UIButton中的圖片與文字上下對齊詳解
- IOS 開發(fā)之UILabel 或者 UIButton加下劃線鏈接
- IOS 通過tag刪除動態(tài)創(chuàng)建的UIButton
- IOS倒計時設(shè)置UIButton標(biāo)題title的抖動問題
- IOS開發(fā)UIButton(左邊圖片右邊文字效果)
- iOS設(shè)置UIButton文字顯示位置和字體大小、顏色的方法
- IOS 解決UIButton 點(diǎn)擊卡頓/延遲的問題
- 詳解iOS App開發(fā)中改變UIButton內(nèi)部控件的基本方法
- iOS - UIButton(UIEdgeInsets)/設(shè)置button上的文字和圖片上下垂直居中對齊
- iOS UIButton擴(kuò)大按鈕響應(yīng)區(qū)域的解決方法
相關(guān)文章
IOS實(shí)現(xiàn)圖片輪播無限循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了IOS實(shí)現(xiàn)圖片輪播無限循環(huán)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03
iOS中管理剪切板的UIPasteboard粘貼板類用法詳解
在iOS中,通過UITextField、UITextView和UIWebView剪切或復(fù)制的內(nèi)容都可以通過UIPasteboard類來管理粘貼操作,下面就為大家?guī)韎OS中管理剪切板的UIPasteboard粘貼板類用法詳解:2016-06-06
iOS實(shí)現(xiàn)聯(lián)系人按照首字母進(jìn)行排序的實(shí)例
下面小編就為大家分享一篇iOS實(shí)現(xiàn)聯(lián)系人按照首字母進(jìn)行排序的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
iOS實(shí)現(xiàn)去除html標(biāo)簽的方法匯總
相信大家在做網(wǎng)站的時候,經(jīng)常會遇到去除html標(biāo)簽的問題,下面這篇文章主要給大家總結(jié)介紹了關(guān)于iOS如何實(shí)現(xiàn)去除html標(biāo)簽的一些方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10
iOS自定義button抖動效果并實(shí)現(xiàn)右上角刪除按鈕
這篇文章主要為大家詳細(xì)介紹了iOS自定義button抖動效果并實(shí)現(xiàn)右上角刪除按鈕的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03
iOS獲取當(dāng)前設(shè)備型號等信息(全)包含iPhone7和iPhone7P
這篇文章主要介紹了iOS獲取當(dāng)前設(shè)備型號設(shè)備信息的總結(jié)包含iPhone7和iPhone7P,包括ios7之前之后的獲取方式,本文接的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下2016-10-10
IOS設(shè)置UIView的邊框?yàn)閳A角詳解及實(shí)例
這篇文章主要介紹了IOS設(shè)置UIView的邊框?yàn)閳A角的相關(guān)資料,需要的朋友可以參考下2017-03-03
searchDisplayController 引起的數(shù)組越界處理辦法
這篇文章主要介紹了searchDisplayController 引起的數(shù)組越界處理辦法,需要的朋友可以參考下2015-07-07

