- (void)viewDidLoad {
[super viewDidLoad];
//定制textField
//键盘风格
self.userNameTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;//标准电话键盘
//键盘外观
self.userNameTextField.keyboardAppearance = UIKeyboardAppearanceLight;//浅色外观
//回车键
self.userNameTextField.returnKeyType = UIReturnKeyJoin;//可以不设置
//首字母是否自动大写
self.userNameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;//不大写
/*
UITextAutocapitalizationTypeNone,
UITextAutocapitalizationTypeWords,单词首字母大写
UITextAutocapitalizationTypeSentences,句子首字母大写
UITextAutocapitalizationTypeAllCharacters,搜友字母大写
*/
//自动更正
self.userNameTextField.autocorrectionType = UITextAutocorrectionTypeYes;
//密文输入
self.userNameTextField.secureTextEntry = NO;
//设置边框样式
self.userNameTextField.borderStyle = UITextBorderStyleRoundedRect;//圆角
//设置清除模式
self.userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//设置左右边框显示视图
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.userPassworldTextField.frame)/3.0, CGRectGetHeight(self.userPassworldTextField.frame))];
nameLabel.text = @"用户名:";
nameLabel.textColor = [UIColor blackColor];
nameLabel.font = [UIFont systemFontOfSize:18];
self.userNameTextField.leftView = nameLabel;
self.userNameTextField.leftViewMode = UITextFieldViewModeAlways;
//再次编辑是否清空
self.userNameTextField.clearsOnBeginEditing = NO;
//设置引用语
self.userNameTextField.placeholder = @"请输入电话号码:";
//设置文本信息
self.userNameTextField.textColor = [UIColor blackColor];
self.userNameTextField.font = [UIFont systemFontOfSize:18];
//其他设置
self.userNameTextField.layer.cornerRadius = 10;
self.userNameTextField.layer.borderWidth = 1;
self.userNameTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
//设置代理
self.userNameTextField.delegate = self;
/*
代理方法里边可以根据需要做很多处理,具体实现不在鳌述
*/
//添加到视图界面
[self.view addSubview:self.userNameTextField];
}
一般情况下都需要写上以下代码以取消第一响应
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.userNameTextField resignFirstResponder];
[self.userPassworldTextField resignFirstResponder];
}
协议方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//当用户按下ruturn,把焦点从textField移开那么键盘就会消失了
[textField resignFirstResponder];
returnYES;
}