iOS階段學習第29天筆記(UITextField的介紹),iosuitextfield
iOS學習(UI)知識點整理
一、關於UITextField的介紹
1)概念: UITextField 是用於接收使用者輸入的一個控制項
2)UITextField 初始化執行個體代碼:
1 //建立一個UItextField執行個體 2 UITextField *textField = [[UITextField alloc] init]; 3 textField.frame = CGRectMake(10, 40, self.view.frame.size.width - 20, 40); 4 textField.backgroundColor = [UIColor lightGrayColor]; 5 //設定textFiled中的文字 6 textField.text = @"使用者名稱"; 7 //設定textFiled的字型 8 textField.font = [UIFont systemFontOfSize:20]; 9 //設定textFiled的文字顏色10 textField.textColor = [UIColor purpleColor];11 [self.view addSubview: textField];
3)textAlignment 設定文本的對齊 例如:
1 //設定textFiled的文本靠左對齊2 textField.textAlignment = NSTextAlignmentLeft;
4)borderStyle 設定文字框樣式 例如:
1 //設定textFiled樣式為無邊框樣式2 textField.borderStyle = UITextBorderStyleNone;3 //borderStyle 有以下幾種類型4 //1、UITextBorderStyleNone,5 //2、UITextBorderStyleLine,6 //3、UITextBorderStyleBezel,7 //4、UITextBorderStyleRoundedRect
5)layer.cornerRadius 設定文字框圓角 例如:
1 textField.layer.cornerRadius = 4.0f;
6)layer.borderWidth 設定文字框邊框寬度 例如:
1 textField.layer.borderWidth = 1;
7)layer.borderColor 設定文字框邊框顏色 例如:
1 textField.layer.borderColor = [UIColor darkGrayColor].CGColor;
8)background 設定文字框背景圖片 例如:
1 UIImage *image = [UIImage imageNamed:@"btnEmojBtn"];2 textField.background = image;
9)設定文字框預設顯示文字(預設灰色字型點擊鍵盤輸入時文字消失)例如:
方法一:placeholder
1 textField.placeholder = @"使用者名稱";
方法二: NSMutableAttributedString
1 NSMutableAttributedString *muAttStr = [[NSMutableAttributedString alloc] initWithString:@"使用者名稱"];2 [muAttStr addAttribute:NSForegroundColorAttributeName value:3 [UIColor blueColor] range:NSMakeRange(0, muAttStr.length)];4 textField.attributedPlaceholder = muAttStr;
10)clearButtonMode 設定文字框的清除內容按鈕顯示模式(即文字框右邊的小叉) 例如:
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
typedef enum { UITextFieldViewModeNever, //從不出現 UITextFieldViewModeWhileEditing, //編輯時出現 UITextFieldViewModeUnlessEditing, //除了編輯外都出現 UITextFieldViewModeAlways //一直出現} UITextFieldViewMode;
11)leftView 設定文字框的左邊視圖 例如:
1 UIView *leftView = [[UIView alloc] init];2 leftView.backgroundColor = [UIColor clearColor];3 leftView.frame = CGRectMake(0, 0, 50, 40);4 textField.leftView = iconImgView;5 //設定文字框左邊視圖的出現方式6 textField.leftViewMode = UITextFieldViewModeAlways;
12)returnKeyType 設定文字框呼出的鍵盤右下角按鈕類型 例如:
1 textField.returnKeyType = UIReturnKeyDone;
13)userInteractionEnabled 設定文字框是否可編輯 例如:
1 //設定文字框不可編輯2 textField.userInteractionEnabled=NO;
14)becomeFirstResponder 設定文字框成為第一響應者即呼出鍵盤 例如:
1 [textField becomeFirstResponder];2 //注意:當設定一個控制項為第一響應者之後,再設定其他為第一響應者無效 第一響應者有且只能有一個
15)resignFirstResponder 設定文字框放棄第一響應者即隱藏鍵盤 例如:
1 [textField resignFirstResponder];
16)secureTextEntry 設定文字框密文模式即密碼框 例如:
1 textField .secureTextEntry = YES;
17)keyboardType 設定文字框呼出鍵盤類型 例如:
1 //設定數字鍵台 2 textField.keyboardType = UIKeyboardTypeNumberPad; 3 4 //keyboardType 有: 5 typedef enum { 6 UIKeyboardTypeDefault, // 預設鍵盤,支援所有字元 7 UIKeyboardTypeASCIICapable, //支援ASCII的預設鍵盤 8 UIKeyboardTypeNumbersAndPunctuation, //標準電話鍵盤,支援+*#字元 9 UIKeyboardTypeURL, //URL鍵盤,支援.com按鈕 只支援URL字元10 UIKeyboardTypeNumberPad, //數字鍵台11 UIKeyboardTypePhonePad, // 電話鍵盤12 UIKeyboardTypeNamePhonePad, //電話鍵盤,也支援輸入人名13 UIKeyboardTypeEmailAddress, //用於輸入電子 郵件地址的鍵盤 14 UIKeyboardTypeDecimalPad, //數字鍵台 有數字和小數點15 UIKeyboardTypeTwitter, //最佳化的鍵盤,方便輸入@、#字元16 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,17 } UIKeyboardType;
18)adjustsFontSizeToFitWidth 設定文字框內容超出文字框範圍時文字自動變化大小 例如:
1 textField.adjustsFontSizeToFitWidth = YES;2 //設定自動縮小顯示的最小字型大小3 textField.minimumFontSize = 20;
19)keyboardAppearance 設定鍵盤外觀 例如:
//深灰 石墨色鍵盤2 textField.keyboardAppearance=UIKeyboardAppearanceAlert;3 typedef enum {4 UIKeyboardAppearanceDefault, //預設面板,淺灰色5 UIKeyboardAppearanceAlert, // 深灰 石墨色 6 } UIReturnKeyType;
20)文字框 內容對齊 例如:
1 //內容的垂直對齊 UITextField繼承自UIControl,此類中有一個屬性contentVerticalAlignment2 textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
21)font 設定文字框字型樣式和大小 例如:
1 textField.font = [UIFont fontWithName:@"Arial" size:20.0f];
22)delegate 設定文字框代理對象 例如:
textField.delegate = self;//文字框設定代理時當前類先必須遵守 UITextFieldDelegate 協議然後實現它的協議方法//它的協議方法有: //返回一個BOOL值,是否允許文字框開始編輯 1、- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;//開始編輯時觸發,文字框將成為第一響應者(first responder ) 2、- (void)textFieldDidBeginEditing:(UITextField *)textField;//返回BOOL值,是否允許文字框結束編輯,當編輯結束,當前文字框會放棄第一響應者身份 3、- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;//當文字框結束編輯時觸發此方法4、- (void)textFieldDidEndEditing:(UITextField *)textField;//當文字框內容改變時觸發此方法,可在此方法中對常值內容做非法字元篩選或限制輸入5、- (BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; //當點擊文字框右邊清除按鈕時觸發此方法6、- (BOOL)textFieldShouldClear:(UITextField *)textField; //當點擊鍵盤右下角按鈕Return 按鈕時觸發此方法 7、- (BOOL)textFieldShouldReturn:(UITextField *)textField;