IOS實現UIButton圖文混排、自訂按鈕按下和正常狀態下不同的背景顏色、根據文字長度自訂UIButton長度,iosuibutton

來源:互聯網
上載者:User

IOS實現UIButton圖文混排、自訂按鈕按下和正常狀態下不同的背景顏色、根據文字長度自訂UIButton長度,iosuibutton

  在一些項目中,我們需要自訂自己的UIButton,使Button上面同時具有圖片和文字描述,實現自訂UIButton的圖文混排。

  首先我們需要定義一個繼承自UIButton的類,同時實現自己的initWithFrame:方法。方法聲明在這個類的標頭檔中。

self = [super initWithFrame:frame];    if (self) {         }    return self;

在if判斷語句中,我們可以實現對按鈕的一些自訂屬性和方法,如按鈕圓角、Title文本、背景顏色等資訊。

self.titleLabel.textAlignment=NSTextAlignmentLeft;

這些都可以根據自己的需要進行簡單設定。

為了上面一些簡單的設定當然不需要自訂一個類,所以下面gc來了!!!!!

假如需要根據UIButton 的文字內容自適應調整UIButton的長度,需要以下幾步:

1.首先自動擷取文本的長度,

NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};        titleTextSize = [self.titleLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, self.frame.size.height) options:NSStringDrawingTruncatesLastVisibleLine attributes:attribute context:nil].size;        [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, titleTextSize.width + cornerRedius*2 +self.frame.size.height , self.frame.size.height)];

上面方法得到的size就是根據系統設定字型大小,所獲得的文字長度和高度的size。擷取後重新調用setFrame方法,重新定義自己的Frame大小,就實現了自適應大小。

2.自訂圖片顯示位置和文字顯示位置

[self setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

使用這個方法設定了UIImage後,你會發現,圖片和文字可能不是你想要的,這個時候,需要重寫UIButton類的兩個方法,如下:

- (CGRect)titleRectForContentRect:(CGRect)contentRect{    CGFloat titleW = contentRect.size.width - btnCornerRedius;    CGFloat titleH = self.frame.size.height;    CGFloat titleX = self.frame.size.height + btnCornerRedius;    CGFloat titleY = 0;    contentRect = (CGRect){{titleX,titleY},{titleW,titleH}};    return contentRect;    }- (CGRect)imageRectForContentRect:(CGRect)contentRect{    CGFloat imageW = self.frame.size.height -5;    CGFloat imageH = self.frame.size.height -5;    CGFloat imageX = btnCornerRedius;    CGFloat imageY = 2.5;    contentRect = (CGRect){{imageX,imageY},{imageW,imageH}};    return contentRect;    }

這裡的btnCornerRedius是我定義的圓角弧度。

重寫這兩個方法後,圖片的高度和文字的位置就在你的掌控之內了。

3、設定Button 的背景顏色。當你需要設定UIButton在按下狀態和普通狀態的不同的顏色,可以使用以下方法:

- (UIImage *)imageWithColor:(UIColor *)color {    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);    UIGraphicsBeginImageContext(rect.size);    CGContextRef context = UIGraphicsGetCurrentContext();        CGContextSetFillColorWithColor(context, [color CGColor]);    CGContextFillRect(context, rect);        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();        return image;}

使用此方法構建一個指定顏色的兩張圖片,然後載入在UIButton的

setBackgroundImage方法中,就可以實現按鈕在按下後不同的兩種顏色的顯示。

本文中設定方法為:

        [self setBackgroundImage:[self imageWithColor:[UIColor colorWithHexString:btnNormalStateHEXCorlor]] forState:UIControlStateNormal];        [self setBackgroundImage:[self imageWithColor:[UIColor colorWithHexString:btnSelectedHEXCorlor]] forState:UIControlStateHighlighted];

注意,上面這段代碼的colorWithHexString:是一個顏色十六進位格式轉化為UIColor的紅綠藍格式的方法,在網上可以搜到,或者直接在

imageWithColor的方法中傳入UIColor。


 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.