iOS開發系列之三 - UITextField 用法小結
// 初始化輸入框並設定位置和大小UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 30)];// 設定輸入框提示textField.placeholder = @"TextField Tip";// 輸入框中預先輸入的文字textField.text = @"預先輸入的文字";// 設定輸入框文本的字型textField.font = [UIFont fontWithName:@"Arial" size:20.0f];// 設定輸入框字型顏色textField.textColor = [UIColor redColor];// 設定輸入框的背景顏色textField.backgroundColor = [UIColor grayColor];// 設定輸入框邊框樣式textField.borderStyle = UITextBorderStyleRoundedRect;// 邊框樣式有以下幾種:// enum {// UITextBorderStyleNone, 無邊框,預設// UITextBorderStyleLine, 有線型邊框// UITextBorderStyleBezel, 有線型邊框和陰影// UITextBorderStyleRoundedRect 有圓角邊框// } UITextBorderStyle;// 設定輸入框是否用於密碼textField.secureTextEntry = NO;// 設定是否有清除按鈕,在什麼時候顯示,用於一次性刪除輸入框中的所有內容textField.clearButtonMode = UITextFieldViewModeWhileEditing;// 清除按鈕樣式有以下幾種:// enum {// UITextFieldViewModeNever, 從不出現// UITextFieldViewModeWhileEditing, 編輯時出現// UITextFieldViewModeUnlessEditing, 除了編輯外都出現// UITextFieldViewModeAlways 一直出現// } UITextFieldViewMode;// 設定自動錯誤修正方式textField.autocorrectionType = UITextAutocorrectionTypeNo;// 自動錯誤修正方式有以下幾種:// enum {// UITextAutocorrectionTypeDefault, 預設// UITextAutocorrectionTypeNo, 不自動錯誤修正// UITextAutocorrectionTypeYes, 自動錯誤修正// } UITextAutocorrectionType;// 設定自動大寫方式textField.autocapitalizationType = UITextAutocapitalizationTypeNone;// 自動大寫方式有以下幾種:// enum {// UITextAutocapitalizationTypeNone, 不自動大寫// UITextAutocapitalizationTypeWords, 單字首大寫// UITextAutocapitalizationTypeSentences, 句子的首字母大寫// UITextAutocapitalizationTypeAllCharacters, 所有字母都大寫// } UITextAutocapitalizationType;// 設定再次編輯是否清空textField.clearsOnBeginEditing = YES;// 設定文本對齊textField.textAlignment = NSTextAlignmentLeft;// iOS7中文本對齊有以下幾種:// enum {// NSTextAlignmentLeft = 0, 靠左對齊,預設// NSTextAlignmentCenter = 1, 置中對齊// NSTextAlignmentRight = 2, 靠右對齊// NSTextAlignmentJustified = 3, 在一個段落的最後一行自然對齊// NSTextAlignmentNatural = 4, 預設對齊// } NSTextAlignment;// 設定字型大小是否自動適應輸入框寬度,預設是保持原來大小,長文本滾動textField.adjustsFontSizeToFitWidth = YES;// 設定自動縮小顯示的最小字型大小textField.minimumFontSize = 20;// 設定鍵盤的樣式textField.keyboardType = UIKeyboardTypeNumberPad;// 鍵盤樣式有以下幾種:// enum {// UIKeyboardTypeDefault, 預設鍵盤,支援所有字元// UIKeyboardTypeASCIICapable, 支援ASCII的預設鍵盤// UIKeyboardTypeNumbersAndPunctuation, 標準電話鍵盤,支援+*#字元// UIKeyboardTypeURL, 只支援URL字元的URL鍵盤,支援.com按鈕// UIKeyboardTypeNumberPad, 數字鍵台// UIKeyboardTypePhonePad, 電話鍵盤// UIKeyboardTypeNamePhonePad, 支援輸入人名的電話鍵盤// UIKeyboardTypeEmailAddress, 電子郵件鍵盤// UIKeyboardTypeDecimalPad, 有數字和小數點的數字鍵台// UIKeyboardTypeTwitter, 最佳化的鍵盤,方便輸入@、#字元// UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,// } UIKeyboardType;// 設定return鍵樣式textField.returnKeyType = UIReturnKeyDone;// return鍵有以下幾種樣式:// enum {// UIReturnKeyDefault, 預設,灰色按鈕,標有Return// UIReturnKeyGo, 標有Go的藍色按鈕// UIReturnKeyGoogle, 標有Google的藍色按鈕,用於搜尋// UIReturnKeyJoin, 標有Join的藍色按鈕// UIReturnKeyNext, 標有Next的藍色按鈕// UIReturnKeyRoute, 標有Route的藍色按鈕// UIReturnKeySearch, 標有Search的藍色按鈕// UIReturnKeySend, 標有Send的藍色按鈕// UIReturnKeyYahoo, 標有Yahoo的藍色按鈕// UIReturnKeyYahoo, 標有Yahoo的藍色按鈕// UIReturnKeyEmergencyCall, 緊急電話按鈕// } UIReturnKeyType; // 設定鍵盤外觀textField.keyboardAppearance = UIKeyboardAppearanceDefault;// 鍵盤外觀有一下兩種:// enum {// UIKeyboardAppearanceDefault, 預設面板,淺灰色// UIKeyboardAppearanceAlert, 深灰,石墨色// } UIReturnKeyType;// 設定代理,用於實現協議textField.delegate = self; // 最右側加圖片是以下代碼,左側類似UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];textField.rightView = image;textField.rightViewMode = UITextFieldViewModeAlways;// 把輸入框加到視圖中[self.view addSubview:textField];// 按return鍵收合鍵盤- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [text resignFirstResponder]; return YES;}