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 //為架構預留的
四、設定按鈕形狀類型
self.loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonWithType: 定義button按鈕的外形
六種定義button類型: 下面有圖解
UIButtonTypeCustom = 0, 無類型
UIButtonTypeRoundedRect, 四個角是圓弧 型的
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
或者:
[Btn.layer setMasksToBounds:YES];
[Btn.layer setCornerRadius:8.0]; //設定矩圓角半徑
[Btn.layer setBorderWidth:1.0]; //邊框寬度
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
[Btn.layer setBorderColor:colorref];//邊框顔色
五、擷取按鈕的屬性
- (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);
程式猿神奇的手,每時每刻,這雙手都在改變着世界的互動方式!
本文轉自當天真遇到現實部落格園部落格,原文連結:http://www.cnblogs.com/XYQ-208910/p/4774082.html,如需轉載請自行聯系原作者