iOS階段學習第29天筆記(UITextField的介紹),iosuitextfield

來源:互聯網
上載者:User

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;     

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.