iOS - UIButton(UIEdgeInsets)/設(shè)置button上的文字和圖片上下垂直居中對(duì)齊
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;
在UIButton中有三個(gè)對(duì)EdgeInsets的設(shè)置:ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets
@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero @property(nonatomic) UIEdgeInsets titleEdgeInsets; // default is UIEdgeInsetsZero @property(nonatomic) BOOL reversesTitleShadowWhenHighlighted; // default is NO. if YES, shadow reverses to shift between engrave and emboss appearance @property(nonatomic) UIEdgeInsets imageEdgeInsets; // default is UIEdgeInsetsZero
UIEdgeInsetsMake
里面的四個(gè)參數(shù)表示距離上邊界、左邊界、下邊界、右邊界的距離,默認(rèn)都為零,title/image在button的正中央
UIKIT_STATIC_INLINE UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) {
UIEdgeInsets insets = {top, left, bottom, right};
return insets;
}
self.view.backgroundColor = [UIColor blackColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//button的類(lèi)型
button.frame = CGRectMake(100, 100,90, 90);//button的frame
button.backgroundColor = [UIColor cyanColor];//button的背景顏色
// [button setBackgroundImage:[UIImage imageNamed:@"man_64.png"] forState:UIControlStateNormal];
// 在UIButton中有三個(gè)對(duì)EdgeInsets的設(shè)置:ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets
[button setImage:[UIImage imageNamed:@"IconHome@2x.png"] forState:UIControlStateNormal];//給button添加image
button.imageEdgeInsets = UIEdgeInsetsMake(5,13,21,button.titleLabel.bounds.size.width);//設(shè)置image在button上的位置(上top,左left,下bottom,右right)這里可以寫(xiě)負(fù)值,對(duì)上寫(xiě)-5,那么image就象上移動(dòng)5個(gè)像素
[button setTitle:@"首頁(yè)" forState:UIControlStateNormal];//設(shè)置button的title
button.titleLabel.font = [UIFont systemFontOfSize:16];//title字體大小
button.titleLabel.textAlignment = NSTextAlignmentCenter;//設(shè)置title的字體居中
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//設(shè)置title在一般情況下為白色字體
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];//設(shè)置title在button被選中情況下為灰色字體
button.titleEdgeInsets = UIEdgeInsetsMake(71, -button.titleLabel.bounds.size.width-50, 0, 0);//設(shè)置title在button上的位置(上top,左left,下bottom,右right)
// [button setContentEdgeInsets:UIEdgeInsetsMake(70, 0, 0, 0)];//
// button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//設(shè)置button的內(nèi)容橫向居中。。設(shè)置content是title和image一起變化
[button addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//button相應(yīng)的事件
-(void)tap {
NSLog(@"tap a button");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"hello" message:@"willingseal" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[alertView show];
}
效果圖:
我這里使用的image是64*64的


--
那就一個(gè)button,這個(gè)button的背景圖片中既有圖片又有文字就可以了,找個(gè)美工幫忙弄一下應(yīng)該很逼真的。。。??!
以上就是本文給大家分享的iOS - UIButton(UIEdgeInsets)/設(shè)置button上的文字和圖片上下垂直居中對(duì)齊,希望大家喜歡。
- iOS UIButton 點(diǎn)擊無(wú)響應(yīng)的解決辦法
- iOS如何將UIButton中的圖片與文字上下對(duì)齊詳解
- IOS 開(kāi)發(fā)之UILabel 或者 UIButton加下劃線(xiàn)鏈接
- IOS 通過(guò)tag刪除動(dòng)態(tài)創(chuàng)建的UIButton
- IOS倒計(jì)時(shí)設(shè)置UIButton標(biāo)題title的抖動(dòng)問(wèn)題
- IOS開(kāi)發(fā)UIButton(左邊圖片右邊文字效果)
- iOS設(shè)置UIButton文字顯示位置和字體大小、顏色的方法
- IOS 解決UIButton 點(diǎn)擊卡頓/延遲的問(wèn)題
- 詳解iOS中UIButton的三大UIEdgeInsets屬性用法
- 詳解iOS App開(kāi)發(fā)中改變UIButton內(nèi)部控件的基本方法
- iOS UIButton擴(kuò)大按鈕響應(yīng)區(qū)域的解決方法
相關(guān)文章
iOS11上Xcode9 AppIcon 圖標(biāo)不顯示
升級(jí)到iOS11系統(tǒng)下自己的項(xiàng)目桌面app圖標(biāo)不見(jiàn)了,是什么原因呢,經(jīng)過(guò)一番查找,終于發(fā)現(xiàn)問(wèn)題所在,現(xiàn)在分享給大家2017-11-11
IOS實(shí)現(xiàn)上滑隱藏NvaigtionBar而下拉則顯示效果
這篇文章給大家介紹了如何實(shí)現(xiàn)APP上滑時(shí)隱藏navigationBar而下拉則又會(huì)顯示,雖然也是隱藏但是效果和其他完全不一樣,因?yàn)橐郧皼](méi)做過(guò)所以試著去實(shí)現(xiàn)一下,現(xiàn)在分享給大家,有需要的可以參考借鑒。2016-09-09
兩種iOS調(diào)用系統(tǒng)發(fā)短信的方法
iOS調(diào)用系統(tǒng)的發(fā)短信功能可以分為兩種:1,程序外調(diào)用系統(tǒng)發(fā)短信。2,程序內(nèi)調(diào)用系統(tǒng)發(fā)短信。第二種的好處是用戶(hù)發(fā)短信之后還可以回到app。這對(duì)app來(lái)說(shuō)非常重要。2016-07-07
IOS中UIWebView加載Loading的實(shí)現(xiàn)方法
最近有朋友問(wèn)我類(lèi)似微信語(yǔ)音播放的喇叭動(dòng)畫(huà)和界面圖片加載loading界面是怎樣實(shí)現(xiàn)的,是不是就是一個(gè)gif圖片呢!我的回答當(dāng)然是否定了,當(dāng)然不排除也有人用gif圖片啊!2015-05-05
iOS tableView右側(cè)索引視圖狀態(tài)獲取的方法實(shí)例
tableView用于顯示一個(gè)垂直滾動(dòng)的單元格數(shù)(通常為可重復(fù)使用的單元格)組成的視圖,這篇文章主要給大家介紹了關(guān)于iOS tableView右側(cè)索引視圖狀態(tài)獲取的相關(guān)資料,需要的朋友可以參考下2021-07-07
SIGPIPE(Signal?13,?Code?0)?異常排查及處理
這篇文章主要為大家介紹了SIGPIPE(Signal?13,?Code?0)?異常排查原因解析及處理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
iOS獲取當(dāng)前設(shè)備型號(hào)等信息(全)包含iPhone7和iPhone7P
這篇文章主要介紹了iOS獲取當(dāng)前設(shè)備型號(hào)設(shè)備信息的總結(jié)包含iPhone7和iPhone7P,包括ios7之前之后的獲取方式,本文接的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
iOS使用GCDSocketManager實(shí)現(xiàn)長(zhǎng)連接的方法
下面想就為大家分享一篇iOS使用GCDSocketManager實(shí)現(xiàn)長(zhǎng)連接的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
iOS開(kāi)發(fā)中使用FMDB來(lái)使程序連接SQLite數(shù)據(jù)庫(kù)
這篇文章主要介紹了iOS開(kāi)發(fā)中使用FMDB來(lái)使程序連接SQLite數(shù)據(jù)庫(kù),SQLite是一個(gè)簡(jiǎn)單的嵌入式數(shù)據(jù)庫(kù),非常適合輕量級(jí)使用,需要的朋友可以參考下2015-11-11

