UI-UIButton、UILable、UITextField总结
UIButton按鈕======================================================
第一、UIButton的定義
UIButton *button=[[UIButton buttonWithType:(UIButtonType);
能夠定義的button類型有以下6種,
typedef enum {
UIButtonTypeCustom = 0, ?自定義風格
UIButtonTypeRoundedRect, ?圓角矩形
UIButtonTypeDetailDisclosure, ?藍色小箭頭按鈕,主要做詳細說明用
UIButtonTypeInfoLight, ?亮色感嘆號
UIButtonTypeInfoDark, ?暗色感嘆號
UIButtonTypeContactAdd, ?十字加號按鈕
} UIButtonType;
第二、設置frame
button1.frame = CGRectMake(20, 20, 280, 40);
[button setFrame:CGRectMake(20,20,50,50)];
第三、button背景色
button1.backgroundColor = [UIColor clearColor];
[button setBackgroundColor:[UIColor blueColor]];
第四、state狀態
forState: 這個參數的作用是定義按鈕的文字或圖片在何種狀態下才會顯現
enum {
UIControlStateNormal = 0, 常規狀態顯現
UIControlStateHighlighted = 1 << 0, 高亮狀態顯現
UIControlStateDisabled = 1 << 1, 禁用的狀態才會顯現
UIControlStateSelected = 1 << 2, 選中狀態
UIControlStateApplication = 0x00FF0000, 當應用程序標志時
UIControlStateReserved = 0xFF000000 為內部框架預留,可以不管
};
@property(nonatomic,getter=isEnabled)BOOL enabled; ? // default is YES. if NO, ignores touch events and subclasses may draw differently
@property(nonatomic,getter=isSelected)BOOL selected; ?// default is NO may be used by some subclasses or by application
@property(nonatomic,getter=isHighlighted)BOOL highlighted;
第五 、設置button填充圖片和背景圖片
[buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
[buttonsetBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
第六、設置button標題和標題顏色
[button1 setTitle: @"點擊" forState:UIControlStateNormal];
[buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
第七、設置按鈕按下會發光
button.showsTouchWhenHighlighted=NO;
第八、添加或刪除事件處理
[button1 addTarget:self action: @selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
第九、 設置按鈕內部圖片間距和標題間距
UIEdgeInsets insets; // 設置按鈕內部圖片間距
insets.top = insets.bottom = insets.right = insets.left = 10;
bt.contentEdgeInsets = insets;
bt.titleEdgeInsets = insets; // 標題間距
? ? ? 第十、 其他
? ? // 設置按鈕為無效按鈕,如果按鈕無效了,按鈕就不再響應用戶了
? ? ?btn.enabled = YES;
? ? ?// 給按鈕添加手勢識別器
? ? ?[btn addGestureRecognizer:tap];
? ??// 添加一個按鈕 ,示例
? ? UIButton *calBtn = [[UIButton alloc]initWithFrame:CGRectMake(50, 200, 200, 40)]; ?// 按鈕大小
? ? calBtn.backgroundColor = [UIColor orangeColor]; // 背景顏色
? ? [calBtn setTitle:@"點我,我就計算" forState:UIControlStateNormal]; ? ? ? ??// 設置默認狀態下的文字
? [calBtn setTitle:@"點我,我就計算" forState:UIControlStateHighlighted]; // 設置高亮狀態下的文字
? ? [calBtn setBackgroundImage:[UIImage imageNamed:@"login_btn_n_Normal"] forState:UIControlStateNormal]; // 設置默認狀態下的背景圖片
? ? [calBtn setBackgroundImage:[UIImage imageNamed:@"logoff_btn_n_Highlighted"] forState:UIControlStateHighlighted]; ??// 設置高亮狀態下的背景圖片
? ? [self.view addSubview:calBtn]; ?// 最會一定要添加按鈕
?【注】圖片的名稱要提前修改好,最好在后面加上分辨是默認狀態還是高亮狀態的單詞
?
十一.UIButton的常見設置*****************************************************************************************
?? ? - (void)setTitle:(NSString *)title forState:(UIControlState)state;
?? ? 設置按鈕的文字
?? ? - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
?? ? 設置按鈕的文字顏色
?? ? - (void)setImage:(UIImage *)image forState:(UIControlState)state;
?? ? 設置按鈕內部的小圖片
?? ? - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
?? ? 設置按鈕的背景圖片
?? ? btn.titleLabel.font = [UIFont systemFontOfSize:13];
?? ? 設置按鈕的文字字體(需要拿到按鈕內部的label來設置)
?? ? - (NSString *)titleForState:(UIControlState)state;
?? ? 獲得按鈕的文字
?? ? - (UIColor *)titleColorForState:(UIControlState)state;
?? ? 獲得按鈕的文字顏色
?? ? - (UIImage *)imageForState:(UIControlState)state;
?? ? 獲得按鈕內部的小圖片
?? ? - (UIImage *)backgroundImageForState:(UIControlState)state;
?? ? 獲得按鈕的背景圖片
?
?
? UILabel標簽============================================================================
? ?UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 300, 160)]; ?// 大小
? ? lbl.backgroundColor = [UIColor lightGrayColor]; // 背景顏色
? ? lbl.textColor = [UIColor blueColor]; // 字體顏色
? ? // lbl.shadowColor = [UIColor redColor]; ? ? ?// 陰影效果,不常用
? ? // lbl.shadowOffset = CGSizeMake(4, -10);
? ? lbl.text = @"宿舍的"; // 添加文字
? ? // 標簽內容對齊方式
? ? lbl.textAlignment = NSTextAlignmentCenter;
? ? // 設置標簽的行數,如果設置為0,表示可以有任意多行
? ? lbl.numberOfLines = 2;
? ? // 當標簽有多行時,設置換行方式 ,默認的是以單詞為單位
? ? lbl.lineBreakMode = NSLineBreakByTruncatingMiddle;? // 如果不能完全顯示,中間會有三個小點
? ? // 設置標簽高亮狀態
? ? lbl.highlighted = YES;
? ? // 設置標簽高亮時字體顏色
? ? lbl.highlightedTextColor = [UIColor purpleColor];
? ? // 允許用戶可以與標簽進行交互
? ? lbl.userInteractionEnabled = YES; ? ? ? //允許用戶交互
? ? // 定義一個點擊手勢識別器對象
? ? UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lblClicked:)];
? ? // 在標簽上添加一個手勢識別器
? ? [lbl addGestureRecognizer:tap];
? ? // ?lbl.enabled = NO;
? ? lbl.adjustsFontSizeToFitWidth = YES;
?? ? // lbl.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
? ? [self.view addSubview:lbl]; //?控件最后都需要添加
【小結】下面的大家可以試著用一下,?實踐是檢驗真理的唯一標準
1.text:設置標簽顯示文本。?
2.attributedText:設置標簽屬性文本。
3.font:設置標簽文本字體。
4.textAlignment:設置標簽文本對齊方式。?
5.lineBreakMode:設置標簽文字過長時的顯示方式,這個屬性使用于label中文本的換行和截短。首先numberofLines必須設置為0,才有效果。
6.enabled:設置文字內容是否可變。?
7.adjustsFontSizeToFitWidth:文字內容自適應標簽寬度。?
8.adjustsLetterSpacingToFitWidth:根據字母的間隔自適應標簽寬度,超出部分以……顯示。?
9.numberOfLines:標簽最多顯示行數。?
10.minimumScaleFactor:設置最小字體,與minimumFontSize相同,minimumFontSize在IOS 6后不能使用。?
11.highlightedTextColor:設置文本高亮顯示顏色,與highlighted一起使用。?
12.shadowColor:設置文本陰影顏色。?
13.shadowColor:設置文本陰影與原文本的偏移量。label.shadowOffset = CGSizeMake(1.0, 5.0);?
14.userInteractionEnabled:設置標簽是否忽略或移除用戶交互。默認為NO。?
15.preferredMaxLayoutWidth:優先選擇標簽布局的最大寬度。?
16.baselineAdjustment:如果adjustsFontSizeToFitWidth屬性設置為YES,這個屬性就來控制文本基線的行為。
17.?backgroundColor 背景顏色
?UILabel的常見設置***********************************************************************************
?@property(nonatomic,copy) ? NSString ? ? ? ? ? *text;
? 顯示的文字
@property(nonatomic,retain) UIFont ? ? ? ? ? ? *font;
?字體
@property(nonatomic,retain) UIColor? ? ? ? ? ? *textColor;
?? ? 文字顏色
?@property(nonatomic)? ? ? ? NSTextAlignment? ? textAlignment;
?? ? 對齊模式(比如左對齊、居中對齊、右對齊)? ?
? ? ?UIFont --> UIFont代表字體,常見創建方法有以下幾個:
?? ? + (UIFont *)systemFontOfSize:(CGFloat)fontSize; ? 系統默認字體
?? ? + (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;? 粗體
?? ? + (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize;? 斜體
?
UITextField文本========================================================
1.enablesReturnKeyAutomatically
默認為No,如果設置為Yes,文本框中沒有輸入任何字符的話,鍵盤右下角的返回按鈕是disabled的。
2.borderStyle
設置邊框樣式,只有設置了才會顯示邊框樣式
? text.borderStyle = UITextBorderStyleRoundedRect;
typedef enum {
? ? UITextBorderStyleNone,?
? ? UITextBorderStyleLine,
? ? UITextBorderStyleBezel,
? ? UITextBorderStyleRoundedRect ?
? } UITextBorderStyle; ??
3.backgroundColor
設置輸入框的背景顏色,此時設置為白色?如果使用了自定義的背景圖片邊框會被忽略掉
text.backgroundColor = [UIColor whiteColor];
4.background
設置背景
? text.background = [UIImage imageNamed:@"xx.png"]; ?// UITextField 的背景,注意只有UITextBorderStyleNone的時候改屬性有效
設置enable為no時,textfield的背景?
? text.disabledBackground = [UIImage imageNamed: @"ff.png"];
5.placeholder
當輸入框沒有內容時,?提示內容為password
? text.placeholder = @"password"; ?// 可以叫他為 占位符
6.font
設置輸入框內容的字體樣式和大小
? text.font = [UIFont fontWithName:@"Arial" size:20.0f];
7.?textColor
設置字體顏色
? text.textColor = [UIColor redColor];
8. clearButtonMode
輸入框中是否有個叉號,在什么時候顯示,用于一次性刪除輸入框中的內容
? text.clearButtonMode = UITextFieldViewModeAlways;?
typedef enum {
? ? UITextFieldViewModeNever, 重不出現
? ? UITextFieldViewModeWhileEditing,?編輯時出現
? ? UITextFieldViewModeUnlessEditing, 除了編輯外都出現
? ? UITextFieldViewModeAlways 一直出現
} UITextFieldViewMode;
9. text
輸入框中一開始就有的文字
? text.text = @"一開始就在輸入框的文字";
10. secureTextEntry
每輸入一個字符就變成點?用來輸入密碼時,設置這個屬性。
? text.secureTextEntry = YES; ? // 暗文
11. clearsOnBeginEditing
再次編輯就清空
? text.clearsOnBeginEditing = YES;?
12. textAlignment
內容對齊方式
? text.textAlignment = UITextAlignmentLeft;
13. contentVerticalAlignment
內容的垂直對齊方式? UITextField繼承自UIControl,此類中有一個屬性contentVerticalAlignment
? text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
14. adjustsFontSizeToFitWidth
設置為YES時文本會自動縮小以適應文本窗口大小.默認是保持原來大小,而讓長文本滾動
? textFied.adjustsFontSizeToFitWidth = YES;
?// 設置自動縮小顯示的最小字體大小
? text.minimumFontSize = 20;
?15. keyboardType
設置鍵盤的樣式
? text.keyboardType = UIKeyboardTypeNumberPad;
typedef enum {
? ? UIKeyboardTypeDefault, ? ? 默認鍵盤,支持所有字符? ? ? ? ?
? ? UIKeyboardTypeASCIICapable, 支持ASCII的默認鍵盤
? ? UIKeyboardTypeNumbersAndPunctuation, 標準電話鍵盤,支持+*#字符
? ? UIKeyboardTypeURL,? ? ? ? ? ? URL鍵盤,支持.com按鈕?只支持URL字符
? ? UIKeyboardTypeNumberPad, ? 數字鍵盤
? ? UIKeyboardTypePhonePad, 電話鍵盤
? ? UIKeyboardTypeNamePhonePad, 電話鍵盤,也支持輸入人名
? ? UIKeyboardTypeEmailAddress, 用于輸入電子?郵件地址的鍵盤? ? ?
? ? UIKeyboardTypeDecimalPad, ? 數字鍵盤?有數字和小數點
? ? UIKeyboardTypeTwitter,? ? ? 優化的鍵盤,方便輸入@、#字符
? ? UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,?
} UIKeyboardType;
16.autocapitalizationType
首字母是否大寫
? text.autocapitalizationType = UITextAutocapitalizationTypeNone;
typedef enum {
? ? UITextAutocapitalizationTypeNone,?不自動大寫
? ? UITextAutocapitalizationTypeWords, 單詞首字母大寫
? ? UITextAutocapitalizationTypeSentences, 句子的首字母大寫
? ? UITextAutocapitalizationTypeAllCharacters,?所有字母都大寫
} UITextAutocapitalizationType;
17. returnKeyType
return鍵變成什么顏色的鍵
?text.returnKeyType =UIReturnKeyDone;
typedef enum {
? ? UIReturnKeyDefault,?默認?灰色按鈕,標有Return
? ? UIReturnKeyGo,? ? 標有Go的藍色按鈕
? ? UIReturnKeyGoogle,標有Google的藍色按鈕,用語搜索
? ? UIReturnKeyJoin,標有Join的藍色按鈕
? ? UIReturnKeyNext,標有Next的藍色按鈕
? ? UIReturnKeyRoute,標有Route的藍色按鈕
? ? UIReturnKeySearch,標有Search的藍色按鈕
? ? UIReturnKeySend,標有Send的藍色按鈕
? ? UIReturnKeyYahoo,標有Yahoo的藍色按鈕
? ? UIReturnKeyYahoo,標有Yahoo的藍色按鈕
? ? UIReturnKeyEmergencyCall,?緊急呼叫按鈕
} UIReturnKeyType;
18. keyboardAppearance
鍵盤外觀
textView.keyboardAppearance=UIKeyboardAppearanceDefault;
typedef enum {
UIKeyboardAppearanceDefault,?默認外觀,淺灰色
UIKeyboardAppearanceAlert, 深灰?石墨色
} UIReturnKeyType;
?19.??rightView
最右側加圖片是以下代碼 ?左側類似
? ? UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
? ? text.rightView=image;
? ? text.rightViewMode = UITextFieldViewModeAlways;?
typedef enum {
? ? UITextFieldViewModeNever,
? ? UITextFieldViewModeWhileEditing,
? ? UITextFieldViewModeUnlessEditing,
? ? UITextFieldViewModeAlways
} UITextFieldViewMode;
20.?editing
是否允許編輯。
21.Stroyboard中:
1、Text?:設置文本框的默認文本。
2、Placeholder?:?可以在文本框中顯示灰色的字,用于提示用戶應該在這個文本框輸入什么內容。當這個文本框中輸入了數據時,用于提示的灰色的字將會自動消失。
3、Background?:
4、Disabled?:?若選中此項,用戶將不能更改文本框內容。
5、接下來是三個按鈕,用來設置對齊方式。
6、Border Style?:?選擇邊界風格。
7、Clear Button?:?這是一個下拉菜單,你可以選擇清除按鈕什么時候出現,所謂清除按鈕就是出一個現在文本框右邊的小?X?,你可以有以下選擇:
? ? 7.1 Never appears?:?從不出現
? ? 7.2 Appears while editing?:?編輯時出現
? ? 7.3 Appears unless editing?:?
? ? 7.4 Is always visible?:?總是可見
8、Clear when editing begins?:?若選中此項,則當開始編輯這個文本框時,文本框中之前的內容會被清除掉。比如,你現在這個文本框?A?中輸入了?"What"?,之后去編輯文本框?B,若再回來編輯文本框?A?,則其中的?"What"?會被立即清除。
9、Text Color?:?設置文本框中文本的顏色。
10、Font?:?設置文本的字體與字號。
11、Min Font Size?:?設置文本框可以顯示的最小字體(不過我感覺沒什么用)
12、Adjust To Fit?:?指定當文本框尺寸減小時,文本框中的文本是否也要縮小。選擇它,可以使得全部文本都可見,即使文本很長。但是這個選項要跟?Min Font Size?配合使用,文本再縮小,也不會小于設定的?Min Font Size?。
接下來的部分用于設置鍵盤如何顯示。
13、Captitalization?:?設置大寫。下拉菜單中有四個選項:
? ? 13.1 None?:?不設置大寫
? ? 13.2 Words?:?每個單詞首字母大寫,這里的單詞指的是以空格分開的字符串
? ? 13.3 Sentances?:?每個句子的第一個字母大寫,這里的句子是以句號加空格分開的字符串
? ? 13.4 All Characters?:?所以字母大寫
14、Correction?:?檢查拼寫,默認是?YES?。
15、Keyboard?:?選擇鍵盤類型,比如全數字、字母和數字等。
16、Appearance:
17、Return Key?:?選擇返回鍵,可以選擇?Search?、?Return?、?Done?等。
18、Auto-enable Return Key?:?如選擇此項,則只有至少在文本框輸入一個字符后鍵盤的返回鍵才有效。
//添加一個簡單的文本框
? ? UITextField *num1Field = [[UITextField alloc]initWithFrame:CGRectMake(20, 100, 100, 40)]; // 大小
? ? num1Field.backgroundColor = [UIColor whiteColor]; ?// 背景顏色
? ? num1Field.tag = 110; ? // 標記tag
? ? num1Field.keyboardType = UIKeyboardTypeNumberPad; // 設置鍵盤
? ? [self.view addSubview:num1Field]; ? // 添加這個文本框 ,最后控件都需要添加
轉載于:https://www.cnblogs.com/GJ-ios/p/5362083.html
總結
以上是生活随笔為你收集整理的UI-UIButton、UILable、UITextField总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到情人吃饭是什么意思
- 下一篇: 如何实现在一个网页中弹出多个不同的小窗口