——————————————configureTextField——————————————————安全
placeholder 英文註解:佔位符; //其實也就是佔了個地方而已,實際TextField是nil,默認顯示灰色字體。app
autocorrectionType 自動校訂,當輸入內容有誤時,在鍵盤上會顯示校訂後的內容less
returnKeyType 返回鍵的類型 :一般有UIReturnKeySend,UIReturnKeySearch,ide
UIReturnKeyJoin等字體
clearButtonMode 清空輸入的字符:ui
UITextFieldViewModeAlways,不爲空,得到焦點與沒有得到焦點都顯示清空按鈕spa
UITextFieldViewModeNever,不顯示清空按鈕3d
UITextFieldViewModeWhileEditing,不爲空,且在編輯狀態時(及得到焦點)顯示清空按鈕orm
UITextFieldViewModeUnlessEditing, 不爲空,且不在編譯狀態時(焦點不在輸入框上)顯示清空按鈕ci
——————————configureTintedTextField(着色)——————————
- (void)configureTintedTextField {
self.tintedTextField.tintColor = [UIColor aapl_applicationBlueColor]; //效果沒出來
self.tintedTextField.textColor = [UIColor aapl_applicationGreenColor];
}
——————————configureSecureTextField(安全輸入)——————————
- (void)configureSecureTextField {
self.secureTextField.secureTextEntry = YES; //密碼輸入
}
——————————configureSpecificKeyboardTextField(鍵盤風格)——————————
- (void)configureSpecificKeyboardTextField {
self.specificKeyboardTextField.keyboardType = UIKeyboardTypeEmailAddress;
}
——————————configureSpecificKeyboardTextField(自定義鍵盤)——————————
- (void)configureCustomTextField {
// Text fields with custom image backgrounds must have no border.
self.customTextField.borderStyle = UITextBorderStyleNone;
self.customTextField.background = [UIImage imageNamed:@"text_field_background"];
// Create a purple button that, when selected, turns the custom text field's text color to purple.
UIImage *purpleImage = [UIImage imageNamed:@"text_field_purple_right_view"];
UIButton *purpleImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
purpleImageButton.bounds = CGRectMake(0, 0, purpleImage.size.width, purpleImage.size.height);
purpleImageButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
[purpleImageButton setImage:purpleImage forState:UIControlStateNormal];
[purpleImageButton addTarget:self action:@selector(customTextFieldPurpleButtonClicked) forControlEvents:UIControlEventTouchUpInside];
self.customTextField.rightView = purpleImageButton;
self.customTextField.rightViewMode = UITextFieldViewModeAlways;
// Add an empty view as the left view to ensure inset between the text and the bounding rectangle.
UIView *leftPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
leftPaddingView.backgroundColor = [UIColor clearColor];
self.customTextField.leftView = leftPaddingView;
self.customTextField.leftViewMode = UITextFieldViewModeAlways;
self.customTextField.placeholder = NSLocalizedString(@"Placeholder text", nil);
self.customTextField.autocorrectionType = UITextAutocorrectionTypeNo;
self.customTextField.returnKeyType = UIReturnKeyDone;
}
#pragma mark - Actions
- (void)customTextFieldPurpleButtonClicked {
self.customTextField.textColor = [UIColor aapl_applicationPurpleColor];
NSLog(@"The custom text field's purple right view button was clicked.");
}
效果圖:
UITextFieldDelegate方法介紹:
#pragma mark - UITextFieldDelegate (set in Interface Builder)
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder]; //when 'return' key pressed 鍵盤右下角返回鍵盤按下時執行
return YES;
}
// 設置輸入框,是否能夠被修改
// NO-將沒法修改,不出現鍵盤
// YES-能夠修改,默認值
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
// 當輸入框得到焦點時,執行該方法。
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"textFieldDidBeginEditing");
}
// 指定是否容許文本字段結束編輯,容許的話,文本字段會失去first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
// 文本框失去first responder 時,執行
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
}
// 指明是否容許根據用戶請求清除內容
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
return YES;
}
// 文本框的文本,是否能被修改
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}