日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

5分鐘 搞定UIButton的文本與圖片的布局

 dxw121 2017-09-14

UIButton內(nèi)部文本和圖片的布局是我們?nèi)粘4a中,不可缺少的部分,按鈕默認(rèn)左邊圖片右邊文本,那要實(shí)現(xiàn)左邊文本,右邊圖片,我們?cè)撛趺唇鉀Q呢,上面圖片,下面文本又該怎么辦呢

其實(shí)很簡(jiǎn)單,今天總結(jié)下,目前主要用兩種方式,一種就是重寫(xiě)按鈕,另一種就是通過(guò)setTitleEdgeInsets和setImageEdgeInsets方法解決

下圖是按鈕默認(rèn)情況下的圖文布局

1.png

左邊文本,右邊圖片
首先介紹重寫(xiě)按鈕吧,新建一個(gè)按鈕繼承UIButton,

- (void)layoutSubviews {    [super layoutSubviews];           CGRect imageRect = self.imageView.frame;      imageRect.size = CGSizeMake(30, 30);          imageRect.origin.x = (self.frame.size.width - 30) ;          imageRect.origin.y = (self.frame.size.height  - 30)/2.0f;                CGRect titleRect = self.titleLabel.frame;             titleRect.origin.x = (self.frame.size.width - imageRect.size.width- titleRect.size.width);             titleRect.origin.y = (self.frame.size.height - titleRect.size.height)/2.0f;             self.imageView.frame = imageRect;    self.titleLabel.frame = titleRect; }

效果如下:

2.png

上面圖片,下面文本
同樣用重寫(xiě)按鈕的方法

- (void)layoutSubviews{     [super layoutSubviews];    CGRect imageRect = self.imageView.frame;     imageRect.size = CGSizeMake(30, 30);     imageRect.origin.x = (self.frame.size.width - 30) * 0.5;     imageRect.origin.y = self.frame.size.height * 0.5 - 40;    CGRect titleRect = self.titleLabel.frame;     titleRect.origin.x = (self.frame.size.width - titleRect.size.width) * 0.5;     titleRect.origin.y = self.frame.size.height * 0.5 ;    self.imageView.frame = imageRect;    self.titleLabel.frame = titleRect; }

效果如下:
![屏幕快照 2016-05-30 10.23.11.png](http://upload-images./upload_images/616981-34430e2f6f66b344.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240

另一種方法就是通過(guò)setTitleEdgeInsets和setImageEdgeInsets方法解決
這種方法的最大好處,就是不要在重寫(xiě)UIButton,直接在新建的UIButton中改變上面兩個(gè)屬性的值就可以達(dá)到我們想要的結(jié)果
左邊文本右邊圖片
代碼如下:

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];     btn1.frame = CGRectMake(50, 100, 80, 40);     [btn1 setImage:[UIImage imageNamed:@'icon_shouye'] forState:UIControlStateNormal];     [btn1 setTitle:@'首頁(yè)' forState:UIControlStateNormal];     [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];     btn1.backgroundColor = [UIColor redColor];    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];     btn.frame = CGRectMake(50, 50, 80, 40);     [btn setImage:[UIImage imageNamed:@'icon_shouye'] forState:UIControlStateNormal];     [btn setTitle:@'首頁(yè)' forState:UIControlStateNormal];     [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];     btn.backgroundColor = [UIColor redColor];    //上左下右     btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.frame.size.width - btn.imageView.frame.origin.x - btn.imageView.frame.size.width, 0, 0);     btn.titleEdgeInsets = UIEdgeInsetsMake(0, -(btn.frame.size.width - btn.imageView.frame.size.width ), 0, 0);     [self.view addSubview:btn1];     [self.view addSubview:btn];

完全顛倒的效果

4.png

上面圖片下面文本
代碼如下:

 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];     btn.frame = CGRectMake(50, 50, 80, 60);     [btn setImage:[UIImage imageNamed:@'icon_shouye'] forState:UIControlStateNormal];     [btn setTitle:@'首頁(yè)的事' forState:UIControlStateNormal];     [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];     btn.backgroundColor = [UIColor redColor];     btn.imageEdgeInsets = UIEdgeInsetsMake(- (btn.frame.size.height - btn.titleLabel.frame.size.height- btn.titleLabel.frame.origin.y),(btn.frame.size.width -btn.titleLabel.frame.size.width)/2.0f -btn.imageView.frame.size.width, 0, 0);     btn.titleEdgeInsets = UIEdgeInsetsMake(btn.frame.size.height-btn.imageView.frame.size.height-btn.imageView.frame.origin.y, -btn.imageView.frame.size.width, 0, 0);     [self.view addSubview:btn];

效果圖:

5.png

關(guān)于setTitleEdgeInsets和setImageEdgeInsets下面進(jìn)行一些解釋?zhuān)?br>UIButton內(nèi)有兩個(gè)控件titleLabel和imageView,可以用來(lái)顯示一個(gè)文本和圖片,這里的圖片區(qū)別于背景圖片。給UIButton設(shè)置了title和image后,它們會(huì)圖片在左邊,文本在圖片右邊顯示。它們兩個(gè)做為一個(gè)整體依賴(lài)于button的contentHorizontalAlignment居左居右或居中顯示。

顯示格式區(qū)分:
1.當(dāng)button.width < image.width時(shí),只顯示被壓縮后的圖片,圖片是按照f(shuō)illXY的方式壓縮。
2.當(dāng)button.width > image.width,且button.width < (image.width text.width)時(shí),圖片正常顯示,文本被壓縮。
3.當(dāng)button.width > (image.width text.width)時(shí),兩者并列默認(rèn)居中顯示,可通過(guò)button的屬性contentHorizontalAlignment改變對(duì)齊方式。

想改變兩個(gè)子控件的顯示位置,可以分別通過(guò)setTitleEdgeInsets和setImageEdgeInsets來(lái)實(shí)現(xiàn)。對(duì)titleLabel和imageView設(shè)置偏移是針對(duì)他當(dāng)前的位置起作用的,并不是針對(duì)距離button邊框的距離的。

typedefNS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {
   UIControlContentHorizontalAlignmentCenter =0,//居中
   UIControlContentHorizontalAlignmentLeft   =1,//居左
   UIControlContentHorizontalAlignmentRight  =2,//居右
   UIControlContentHorizontalAlignmentFill   =3,//

想兩改變兩個(gè)子控件的顯示位置,可以分別通過(guò)setTitleEdgeInsets和setImageEdgeInsets來(lái)實(shí)現(xiàn)。需要注意的是,對(duì)titleLabel和imageView設(shè)置偏移,是針對(duì)它當(dāng)前的位置起作用的,并不是針對(duì)它距離button邊框的距離的。感覺(jué)設(shè)置不設(shè)置UIControlContentHorizontalAlignmentCenter居中都沒(méi)有影響,這個(gè)網(wǎng)上也找了些相關(guān)的信息,感覺(jué)都沒(méi)有說(shuō)到重點(diǎn),我這里也沒(méi)有完全理解透徹,之前都是在設(shè)置setTitleEdgeInsets和setImageEdgeInsets這些參數(shù)時(shí)都是不停的嘗試得到的結(jié)果。目前這是我理解后,代碼實(shí)現(xiàn)最后的答案,希望可以幫到大家。



文章轉(zhuǎn)自 Migi000的簡(jiǎn)書(shū)

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多