UITextField 通常用於接收外部的文字輸入,也就是我們常見的輸入框。下面用程式碼詳細說明它的各項屬性與方法。
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *textField = [[UITextField alloc] init];
textField.frame = CGRectMake(50, 50, 200, 200);
// 文字相關設定
textField.text = nil; // 輸入框中的文本,預設為空
textField.attributedText = nil; // 屬性字符串
textField.defaultTextAttributes = nil; // 預設屬性字符串
textField.attributedPlaceholder = nil; // 預設顯示的屬性字符串
textField.textColor = [UIColor blackColor]; // 文本顏色
textField.backgroundColor = [UIColor greenColor];
textField.font = [UIFont systemFontOfSize:18.0]; // 字體,預設 12pt
// 密碼框:每輸入一個字符就變成圓點
textField.secureTextEntry = NO;
// 文字對齊方式
textField.textAlignment = NSTextAlignmentNatural;
// 佔位提示文字(約 70% 透明度)
textField.placeholder = @"請輸入文字";
// 外框類型:None 無邊框、Line 方角實線、Bezel、RoundedRect 圓角矩形
textField.borderStyle = UITextBorderStyleNone;
// 字體最小值與是否自動縮放以適應寬度
textField.minimumFontSize = 18.0;
textField.adjustsFontSizeToFitWidth = NO;
// 鍵盤樣式:URL 鍵盤
textField.keyboardType = UIKeyboardTypeURL;
// 再次編輯時是否清空內容
textField.clearsOnBeginEditing = NO;
// 首字母大寫類型
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
// 清除按鈕(叉號)的顯示時機
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
// 是否自動糾錯
textField.autocorrectionType = UITextAutocorrectionTypeNo;
// return 鍵的類型
textField.returnKeyType = UIReturnKeyDefault;
// 鍵盤外觀:淺灰、深灰石墨色
textField.keyboardAppearance = UIKeyboardAppearanceDefault;
// 左側裝飾 view
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
image.bounds = CGRectMake(0, 0, 10, 10);
textField.leftView = image;
textField.leftViewMode = UITextFieldViewModeAlways;
// 右側裝飾 view
UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2"]];
image1.bounds = CGRectMake(0, 0, 10, 10);
textField.rightView = image1;
textField.rightViewMode = UITextFieldViewModeAlways;
textField.delegate = self;
[self.view addSubview:textField];
}
若需要自訂各區域的外觀,可以重寫以下方法:borderRectForBounds:(邊緣區域)、textRectForBounds:(文字區域)、placeholderRectForBounds:(佔位符區域)、editingRectForBounds:(編輯區域)、clearButtonRectForBounds:(清除按鈕位置)、leftViewRectForBounds:/rightViewRectForBounds:(左右 view 區域),以及 drawTextInRect: 與 drawPlaceholderInRect:(自訂繪製文字與佔位符)。
// 是否允許開始編輯,返回 NO 則無法編輯、不出鍵盤
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return YES;
}
// 開始編輯、獲得焦點時調用
- (void)textFieldDidBeginEditing:(UITextField *)textField {
}
// 是否允許結束編輯,返回 NO 可阻止 textField 消失
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
return YES;
}
// 結束編輯後調用
- (void)textFieldDidEndEditing:(UITextField *)textField {
}
// 每次字符變化時調用,可藉此攔截並決定是否允許修改
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return YES;
}
// 是否允許清除內容
- (BOOL)textFieldShouldClear:(UITextField *)textField {
return YES;
}
// 是否允許按下 return 鍵時結束編輯(記得調用 resignFirstResponder 收起鍵盤)
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
透過這些代理方法,就能精準控制輸入框的編輯行為,例如限制輸入內容、按下 return 時收起鍵盤、或是在特定條件下才允許清除文字。