UIButton 內部預設有一個 UIImageView 與一個 UILabel,可分別透過下面的屬性存取:
@property(nonatomic,readonly,retain) UIImageView *imageView; @property(nonatomic,readonly,retain) UILabel *titleLabel;
UIButton 之所以能顯示文字,完全是因為它內部的 titleLabel;也就是說,setTitle:forState: 設定的字串會顯示到 titleLabel 上,而 setImage:forState: 設定的圖片則顯示到內部的 imageView 上。
注意:
1. 設定按鈕的文字或文字顏色,必須用下面的方法:
- (void)setTitle:(NSString *)title forState:(UIControlState)state; - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
警告:不能直接透過 titleLabel 設定文字與文字顏色,以下寫法是錯誤的:
button.titleLabel.text = @"12323"; button.titleLabel.textColor = [UIColor redColor];
2. 設定按鈕內部的小圖片,必須用下面的方法:
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
警告:不能直接透過 imageView 設定圖片,以下寫法是錯誤的:
button.imageView.image = [UIImage imageNamed:@"abc.png"];
複習完基礎後,我們進入本文主題。
改變 UIButton 內部控件
當我們為 UIButton 設定 title 和 image 後,它就相當於由一個 UIButtonLabel 加一個 UIImageView 組成,但兩者預設的排版是固定的:左邊是 image,右邊是 label。如果我們想讓圖片顯示在上方、文字顯示在下方,該怎麼做呢?
我們可以完全自訂一個控件來實現,也可以在 UIButton 的基礎上改變它的內部子控件。這裡採用第二種方法。
1. 首先,直覺上會想透過 imageView 和 titleLabel 屬性來改變子控件的位置。我們先列印看看這個 button 的內部結構:
NSLog(@"%@", self.button.subviews);
結果是一個空陣列!這是因為 UIButton 內部的子控件採用「懶加載」,也就是說如果沒有使用到相應的子控件,它們是不會被載入的。
於是我們重新為這兩個控件的 frame 賦值,這樣不僅用到了這兩個控件使其載入,也能順便觀察能否直接改變 frame 來重新排列。但如果你這麼做,會發現實際顯示的 button 內部並沒有改變——說明直接改變 UIButton 內部控件的 frame 無法達到重新排列的目的。
接著再次列印子控件:
NSLog(@"%@", self.button.subviews);
這次會發現 subviews 陣列裡有了一個 UIImageView 加一個 UIButtonLabel(因為前面用到了它們,觸發了懶加載)。可是仔細觀察會發現,這兩個控件的 frame 明明是我們剛才賦的值,顯示出來的卻又不是按這個 frame 呈現。這是因為 UIButton 在顯示時會根據 imageView 和 titleLabel 裡的內容重新計算大小,所以即使改變了子控件的 frame,也無法真正更改它們的位置與尺寸。
2. 第二種思路是繼承 UIButton,在原有按鈕的基礎上進行改變。例如建立一個 UIButton 的子類 CYLButton,在其實作檔中實作下面的方法:
- (CGRect)titleRectForContentRect: (CGRect)contentRect // 控制 label 顯示在哪和大小
{
return CGRectMake(0, contentRect.size.width, contentRect.size.width, contentRect.size.height - contentRect.size.width);
}
- (CGRect)imageRectForContentRect: (CGRect)contentRect // 控制 image 顯示在哪和大小
{
return CGRectMake(0, 0, contentRect.size.width, contentRect.size.width);
}
// contentRect 一般代表 UIButton 的 bounds.size
// 我們也可以在 initWithFrame: 方法中設定 UIButton 內部控件的屬性
- (instancetype)initWithFrame: (CGRect)frame
{
if (self = [super initWithFrame: frame]) {
self.titleLabel.backgroundColor = [UIColor blueColor];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.imageView.backgroundColor = [UIColor yellowColor];
}
return self;
}
這種方法能滿足需求,但也有弊端:如果在一個方法中設定的某些值想在另一個方法中也用到,就會不太方便。
3. 更好的方法是重寫 layoutSubviews 方法,因為它可以很方便地調整子控件。
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat imageW = self.bounds.size.width;
CGFloat imageH = imageW;
self.imageView.frame = CGRectMake(0, 0, imageW, imageH);
CGFloat titleY = imageH;
CGFloat titleW = imageW;
CGFloat titleH = self.bounds.size.height - titleY;
self.titleLabel.frame = CGRectMake(0, titleY, titleW, titleH);
}
你可能會覺得很奇怪:剛才在類別外面我們也改變了 imageView 和 titleLabel 的 frame 卻毫無作用,為什麼在 layoutSubviews 方法裡同樣修改卻能生效?
因為剛才在外部修改子控件的 frame 後,當執行到內部的 layoutSubviews 時,會重新把它們的 frame 設為 image 和 title 對應的大小;而現在我們直接在 layoutSubviews 中修改它們的 frame,等於覆蓋了「設為預設大小」這一步,所以能成功,而且因為在同一個方法中,變數也能共享。
另外要注意:如果繼承自 UIButton(例如 CYLButton),當有了資料模型、想在 setter 方法中為子控件賦值時,不能直接這樣寫:
self.imageView = ... self.text = ...
因為 CYLButton 繼承自 UIButton,無論是 image 還是 title 都是分狀態的,所以要這樣:
[self setImage...]; [self setTitle...];
也就是說,能不能直接修改,取決於這個屬性分不分狀態。如果分狀態,就不能直接修改。