——————————————configureTextField——————————————————
placeholder 英文注解:占位符; //其实也就是占了个地方而已,实际TextField是nil,默认显示灰色字体。
autocorrectionType 自动校正,当输入内容有误时,在键盘上会显示校正后的内容
returnKeyType 返回键的类型 :通常有UIReturnKeySend,UIReturnKeySearch,
UIReturnKeyJoin等
clearButtonMode 清空输入的字符:
UITextFieldViewModeAlways,不为空,获得焦点与没有获得焦点都显示清空按钮
UITextFieldViewModeNever,不显示清空按钮
UITextFieldViewModeWhileEditing,不为空,且在编辑状态时(及获得焦点)显示清空按钮
UITextFieldViewModeUnlessEditing, 不为空,且不在编译状态时(焦点不在输入框上)显示清空按钮
——————————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;
}