失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > IOS 单行文本输入框 UITextField 使用

IOS 单行文本输入框 UITextField 使用

时间:2021-08-15 20:18:57

相关推荐

IOS 单行文本输入框 UITextField 使用

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 使用》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。