UITextField 继承 UIControl 类,只支持单行输入和显示,可输入密码类型。支持实现代理 UITextFieldDelegate
属性
API
- (BOOL)endEditing:(BOOL)force;
是否强制取消当前输入行为代理协议函数
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
当开始编辑前,返回NO可以阻止编辑- (void)textFieldDidBeginEditing:(UITextField *)textField
当编辑输入结束触发(BOOL)textFieldShouldEndEditing:(UITextField *)textField
结束编辑前,返回NO可以阻止编辑结束(void)textFieldDidEndEditing:(UITextField *)textField
编辑结束- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
当输入内容发生改变触发,range表示改变位置和长度。返回NO可阻止改变- (void)textFieldDidChangeSelection:(UITextField *)textField
输入内容发生改变后触发,IOS13支持。- (BOOL)textFieldShouldClear:(UITextField *)textField
当内容发生清除触发,返回NO阻止清除(BOOL)textFieldShouldReturn:(UITextField *)textField
当按下回车键触发,返回NO可阻止默认行为参考代码
UITextField* _textField = [[UITextField alloc] init];// 设置位置_textField.frame = CGRectMake(50, 100, 300, 60);// 设置圆角边框风格_textField.borderStyle = UITextBorderStyleRoundedRect;// 设置值_textField.text = @"";// 设置提示语_textField.placeholder = @"请输入用户名";// 设置键盘类型_textField.keyboardType = UIKeyboardAppearanceDefault;// 设置代理_textField.delegate = self;// 设置是否为密码类型_textField.secureTextEntry = NO;UITextField* _passwdText = [[UITextField alloc] init];_passwdText.frame = CGRectMake(50, 200, 300, 60);_passwdText.borderStyle = UITextBorderStyleRoundedRect;_passwdText.placeholder = @"请输入密码";_passwdText.keyboardType = UIKeyboardAppearanceDefault;_passwdText.secureTextEntry = YES;[self.view addSubview:_textField];[self.view addSubview:_passwdText];
如果觉得《IOS 单行文本输入框 UITextField 使用》对你有帮助,请点赞、收藏,并留下你的观点哦!