如何布局包含Image和Title的UIButton

介紹兩種管理UIButton中的Image和Title的方法

1憨琳、自定義UIButton

#import "BBImageButton.h"

@implementation BBImageButton

- (instancetype)initWithTitle:(NSString *)title
{
    self = [super initWithFrame:CGRectZero];
    if (self) {
        [self setTitle:title forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateSelected];
        // 設(shè)置button為Selected狀態(tài)時坯苹,點擊button時篷角,圖片不出現(xiàn)閃動
        [self setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:(UIControlStateSelected | UIControlStateHighlighted)];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.imageView.frame), CGRectGetHeight(self.imageView.frame));
    self.imageView.center = CGPointMake(CGRectGetWidth(self.frame) / 2, self.imageView.center.y);
    self.titleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.imageView.frame) + 10, CGRectGetWidth(self.titleLabel.frame), CGRectGetHeight(self.titleLabel.frame));
    self.titleLabel.center = CGPointMake(self.imageView.center.x, self.titleLabel.center.y);
}

@end

如下圖所示:


1.png

左右布局

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat x =  CGRectGetWidth(self.frame) * 0.5 - (CGRectGetWidth(self.titleLabel.frame) - CGRectGetWidth(self.imageView.frame));
    self.titleLabel.frame = CGRectMake(x, self.titleLabel.frame.origin.y, CGRectGetWidth(self.titleLabel.frame), CGRectGetHeight(self.titleLabel.frame));
    self.imageView.frame = CGRectMake(CGRectGetMaxX(self.titleLabel.frame), self.imageView.frame.origin.y, CGRectGetWidth(self.imageView.frame), CGRectGetHeight(self.imageView.frame));
}

如下圖所示:


2.png

在這里面有個小技巧逛钻,如果想要文字跟圖片有一定間距的話,不一定非得改變它的坐標芜壁,可以在title賦值的時候這樣處理功茴,“加一個空格”庐冯,作為程序猿,我們不應(yīng)該放棄任何一個偷懶的機會孽亲。

title = [NSString stringWithFormat:@"%@ ", title];

如下圖所示:


3.png

2坎穿、UIButton中的titleEdgeInsets和imageEdgeInsets可以管理button中image和title的布局。

默認情況下,是圖片在左,文字在右,垂直居中顯示,如下圖:

button.titleEdgeInsets = UIEdgeInsetsZero;
button.imageEdgeInsets = UIEdgeInsetsZero;
11.png

設(shè)置如下布局后,圖片和文字都居中顯示返劲。

button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width, 0, 0);
 // button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, button.titleLabel.frame.size.width);
 // 由于iOS8中titleLabel的size為0玲昧,用上面這樣設(shè)置有問題,修改一下即可
 button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -button.titleLabel.intrinsicContentSize.width);
22.png

如果想圖片在上,文字在下,水平居中顯示,則按下面設(shè)置即可:

button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width, -button.imageView.frame.size.height, 0);
// button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.frame.size.height, 0, 0, -button.titleLabel.frame.size.width);
// 由于iOS8中titleLabel的size為0篮绿,用上面這樣設(shè)置有問題孵延,修改一下即可
button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.intrinsicContentSize.height, 0, 0, -button.titleLabel.intrinsicContentSize.width);
33.png

如果覺得圖片和文字離的太近了,稍微分開一點:

CGFloat offset = 40.0f;
button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width, -button.imageView.frame.size.height-offset/2, 0);
// button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.frame.size.height-offset/2, 0, 0, -button.titleLabel.frame.size.width);
// 由于iOS8中titleLabel的size為0亲配,用上面這樣設(shè)置有問題尘应,修改一下即可
button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.intrinsicContentSize.height-offset/2, 0, 0, -button.titleLabel.intrinsicContentSize.width);
44.png

文字左對齊,圖片右對齊

button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width - button.frame.size.width + button.titleLabel.intrinsicContentSize.width, 0, 0);
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -button.titleLabel.frame.size.width - button.frame.size.width + button.imageView.frame.size.width);
55.png

參考文章:
如何布局包含Image和Title的UIButton
UIButton的Image和Title的布局

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吼虎,一起剝皮案震驚了整個濱河市犬钢,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌思灰,老刑警劉巖玷犹,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異洒疚,居然都是意外死亡歹颓,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門油湖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來巍扛,“玉大人,你說我怎么就攤上這事乏德〉缦妫” “怎么了?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵鹅经,是天一觀的道長寂呛。 經(jīng)常有香客問我,道長瘾晃,這世上最難降的妖魔是什么贷痪? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮蹦误,結(jié)果婚禮上劫拢,老公的妹妹穿的比我還像新娘肉津。我一直安慰自己,他們只是感情好舱沧,可當我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布妹沙。 她就那樣靜靜地躺著,像睡著了一般熟吏。 火紅的嫁衣襯著肌膚如雪距糖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天牵寺,我揣著相機與錄音悍引,去河邊找鬼。 笑死帽氓,一個胖子當著我的面吹牛趣斤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播黎休,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼浓领,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了势腮?” 一聲冷哼從身側(cè)響起联贩,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎嫉鲸,沒想到半個月后撑蒜,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡玄渗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了藤树。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片浴滴。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡岁钓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出屡限,到底是詐尸還是另有隱情品嚣,我是刑警寧澤钧大,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布翰撑,位于F島的核電站,受9級特大地震影響啊央,放射性物質(zhì)發(fā)生泄漏眶诈。R本人自食惡果不足惜涨醋,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望逝撬。 院中可真熱鬧浴骂,春花似錦、人聲如沸宪潮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽坎炼。三九已至愧膀,卻和暖如春拦键,著一層夾襖步出監(jiān)牢的瞬間谣光,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工芬为, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留萄金,地道東北人。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓媚朦,卻偏偏與公主長得像氧敢,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子询张,可洞房花燭夜當晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容