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);