標籤:
UIButton的詳細介紹:
一、按鈕具有的屬性:
@property(nonatomic,readonly) UIButtonType buttonType; //按鈕類型
@property(nonatomic,readonly,retain) NSString *currentTitle; //按鈕當前文字
@property(nonatomic,readonly,retain) UIColor *currentTitleColor; //按鈕當前文字顏色
@property(nonatomic,readonly,retain) UIColor *currentTitleShadowColor; //按鈕文字當前陰影顏色
@property(nonatomic,readonly,retain) UIImage *currentImage; //按鈕當前前景圖片
@property(nonatomic,readonly,retain) UIImage *currentBackgroundImage; //按鈕當前背景圖片
@property(nonatomic,readonly,retain) NSAttributedString *currentAttributedTitle //按鈕文字當前屬性
@property(nonatomic,readonly,retain) UILabel *titleLabel //按鈕標籤
@property(nonatomic,readonly,retain) UIImageView *imageView //按鈕視圖
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment; //按鈕垂直放置方式
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment; //按鈕水平放置方式
@property(nonatomic,readonly) UIControlState //按鈕狀態類型
二、設定按鈕的屬性值
- (void)setTitle:(NSString *)title forState:(UIControlState)state; //設定按鈕文字內容
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state //設定按鈕文字顏色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state //設定按鈕文字陰影顏色
- (void)setImage:(UIImage *)image forState:(UIControlState)state; //設定按鈕前景圖片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state //設定按鈕背景圖片
- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state //設定按鈕文字屬性
三、按鈕的狀態類型
按鈕類型UIControlState:
UIControlStateNormal //正常類型
UIControlStateHighlighted //高亮類型
UIControlStateDisabled //禁用類型
UIControlStateSelected //選中類型
UIControlStateApplication //當應用程式識別碼使用時
UIControlStateReserved //為架構預留的
四、擷取按鈕的屬性
- (NSString *)titleForState:(UIControlState)state; //擷取按鈕文字
- (UIColor *)titleColorForState:(UIControlState)state; //擷取按鈕文字顏色
- (UIColor *)titleShadowColorForState:(UIControlState)state; //擷取按鈕文字陰影顏色
- (UIImage *)imageForState:(UIControlState)state; //擷取按鈕前景圖片
- (UIImage *)backgroundImageForState:(UIControlState)state; //擷取按鈕背景圖片
- (NSAttributedString *)attributedTitleForState:(UIControlState)state; //擷取按鈕文字屬性
五、按鈕文字放置方式
垂直放置:
UIControlContentVerticalAlignmentCenter //置中
UIControlContentVerticalAlignmentTop //置頂
UIControlContentVerticalAlignmentBottom //置底
UIControlContentVerticalAlignmentFill //填充
水平放置:
UIControlContentHorizontalAlignmentCenter //置中
UIControlContentHorizontalAlignmentLeft //居左
UIControlContentHorizontalAlignmentRight //居右
UIControlContentHorizontalAlignmentFill //填充
說明:
(1) 由於按鈕有狀態類型之分,所以,在給按鈕添加文字時,使用button.TitleLabel.Text = @“按鈕”這種賦值方式是無效的,在視圖中不會顯示出來,應該使用[button setTitle:(NSString *)title forState:(UIControlState)
state]這種方式才是有效地。同樣設定文字的顏色也是如此:
設定UIButton上字型的顏色設定UIButton上字型的顏色,不是用:
[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];
而是用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
(2)擷取按鈕的文字,應該使用[button currentTitle],如果使用button.titleLabel.Text,其結果並不是你設定的文字內容。同樣擷取文字的顏色也是如此.[button currentTitleColor]
(3)設定按鈕上的字型的大小
[button setFont: [UIFont systemFontSize: 14.0]]; //這種可以用來設定字型的大小,但是可能會在 將來的SDK版本中去除改方法
button.titleLabel.font = [UIFont fontWithName:(NSString*)fontName size:14.0]; //應該使用
或者
button.TitleLabel.font = [UIFont systemFontOfSize: 14.0]; //應該使用
(4) 有些時候我們想讓UIButton的title居靠左對齊
button.textLabel.textAlignment = UITextAlignmentLeft //是沒有作用的,我們需要設定
button.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft; //顯示居左
但是問題又出來,文字會緊貼到做邊框,我們可以設定使文字距離左邊框保持10個像素的距離。
button.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
Objective-C:UIButton按鈕的詳解