一酪耕、按鈕具有的屬性:
@property(nonatomic,readonly)?UIButtonType buttonType; ?//按鈕形狀類型
@property(nonatomic,readonly,retain)?NSString?*currentTitle;?? ?//按鈕當(dāng)前文字
@property(nonatomic,readonly,retain)?UIColor??*currentTitleColor;? ? ?//按鈕當(dāng)前文字顏色
@property(nonatomic,readonly,retain)?UIColor??*currentTitleShadowColor; ?//按鈕文字當(dāng)前陰影顏色
@property(nonatomic,readonly,retain)?UIImage??*currentImage;?? ? ? ? ? ? //按鈕當(dāng)前前景圖片
@property(nonatomic,readonly,retain)?UIImage??*currentBackgroundImage;?? ?//按鈕當(dāng)前背景圖片
@property(nonatomic,readonly,retain)?NSAttributedString?*currentAttributedTitle //按鈕文字當(dāng)前屬性
@property(nonatomic,readonly,retain)?UILabel?? ??*titleLabel ? ?//按鈕標(biāo)簽
@property(nonatomic,readonly,retain)?UIImageView *imageView ?//按鈕視圖
@property(nonatomic)?UIControlContentVerticalAlignment contentVerticalAlignment;?? ?//按鈕垂直放置方式
@property(nonatomic)?UIControlContentHorizontalAlignment contentHorizontalAlignment; //按鈕水平放置方式
@property(nonatomic,readonly)?UIControlState ?//按鈕狀態(tài)類型
二导梆、設(shè)置按鈕的屬性值
- (void)setTitle:(NSString *)title forState:(UIControlState)state; ? //設(shè)置按鈕文字內(nèi)容
- (void)setTitleColor:(UIColor?*)color forState:(UIControlState)state ?//設(shè)置按鈕文字顏色
- (void)setTitleShadowColor:(UIColor?*)color forState:(UIControlState)state ?//設(shè)置按鈕文字陰影顏色
- (void)setImage:(UIImage?*)image forState:(UIControlState)state;? ?//設(shè)置按鈕前景圖片
- (void)setBackgroundImage:(UIImage?*)image forState:(UIControlState)state ?//設(shè)置按鈕背景圖片
- (void)setAttributedTitle:(NSAttributedString?*)title forState:(UIControlState)state ?//設(shè)置按鈕文字屬性
三、按鈕的狀態(tài)類型
按鈕類型UIControlState:
UIControlStateNormal?? ? ? ? ?//正常類型
UIControlStateHighlighted??? //高亮類型
UIControlStateDisabled?? ? ? //禁用類型
UIControlStateSelected?? ?? ?//選中類型
UIControlStateApplication?? //當(dāng)應(yīng)用程序標(biāo)識(shí)使用時(shí)
UIControlStateReserved?? ? ?//為框架預(yù)留的
四迂烁、設(shè)置按鈕形狀類型
self.loginBtn?=?[UIButton?buttonWithType:UIButtonTypeRoundedRect];
buttonWithType:??定義button按鈕的外形
六種定義button類型:?下面有圖解
UIButtonTypeCustom?=?0,????無類型
UIButtonTypeRoundedRect,????四個(gè)角是圓弧???型的
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
或者:
[Btn.layer setMasksToBounds:YES];
[Btn.layer setCornerRadius:8.0]; //設(shè)置矩圓角半徑
[Btn.layer setBorderWidth:1.0];?? //邊框?qū)挾?/p>
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) 由于按鈕有狀態(tài)類型之分藏斩,所以,在給按鈕添加文字時(shí)却盘,使用button.TitleLabel.Text = @“按鈕”這種賦值方式是無效的,在視圖中不會(huì)顯示出來,應(yīng)該使用[button?setTitle:(NSString *)title forState:(UIControlState)
state]這種方式才是有效地狰域。同樣設(shè)置文字的顏色也是如此:
設(shè)置UIButton上字體的顏色設(shè)置UIButton上字體的顏色,不是用:
[btn.titleLabel?setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor?redColor];
而是用:
[btn?setTitleColor:[UIColor?blackColor]forState:UIControlStateNormal];
(2)獲取按鈕的文字黄橘,應(yīng)該使用[button currentTitle],如果使用button.titleLabel.Text兆览,其結(jié)果并不是你設(shè)置的文字內(nèi)容。同樣獲取文字的顏色也是如此.[button currentTitleColor]
(3)設(shè)置按鈕上的字體的大小
[button?setFont:?[UIFont?systemFontSize:?14.0]];?//這種可以用來設(shè)置字體的大小塞关,但是可能會(huì)在將來的SDK版本中去除改方法
button.titleLabel.font = [UIFont fontWithName:(NSString*)fontName size:14.0]; //應(yīng)該使用
或者
button.TitleLabel.font?=?[UIFont?systemFontOfSize:?14.0]; ? ?//應(yīng)該使用
(4)有些時(shí)候我們想讓UIButton的title居左對(duì)齊
button.textLabel.textAlignment?=?UITextAlignmentLeft ?//是沒有作用的抬探,我們需要設(shè)置
button.contentHorizontalAlignment?=?UIControlContentHorizonAlignmentLeft; //顯示居左
但是問題又出來,文字會(huì)緊貼到做邊框帆赢,我們可以設(shè)置使文字距離左邊框保持10個(gè)像素的距離小压。
button.contentEdgeInsets?=?UIEdgeInsetsMake(0,10,?0,?0);
程序猿神奇的手,每時(shí)每刻椰于,這雙手都在改變著世界的交互方式怠益!
原文地址: http://www.cnblogs.com/XYQ-208910/p/4774082.html