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 //選中型別
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));
[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.titlelabel.font = [uifont fontwithname:(nsstring*)fontname size:14.0]; //應該使用
或者 button.titlelabel.font = [uifont systemfontofsize: 14.0]; //應該使用
(4) 有些時候我們想讓uibutton的title居左對齊
使文字距離左邊框保持10個畫素的距離。
iOS UIButton簡單的使用介紹
1.最簡單的乙個建立方式 uibutton btn uibutton alloc init 2.常用建立方式,在建立button的同時設定type uibutton btn1 uibutton buttonwithtype uibuttontypecustom 3.設定button的文字 在使用中需...
ios UIButton設定高亮狀態下的背景色
一,通過按鈕的事件來設定背景色 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 void viewdidload button1普通狀態下的背景色 void button1backgroundnormal uibutton sende...
ios UIButton設定高亮狀態下的背景色
一,通過按鈕的事件來設定背景色12 3456 78910 1112 1314 1516 1718 1920 2122 void viewdidload button1普通狀態下的背景色 void button1backgroundnormal uibutton sender button1高亮狀態下...