UITextField 继承 UIControl 类,只反对单行输出和显示,可输出明码类型。反对实现代理 UITextFieldDelegate
属性
名称 | 类型 | 阐明 | 默认值 |
---|---|---|---|
text | NSString | 文本输出值 | |
textColor | UIColor | 文本色彩 | |
UIFont | UIFont | 文本大小 | |
textAlignment | NSTextAlignment | 文本方向 | NSLeftTextAlignment |
borderStyle | UITextBorderStyle | 边框格调 | UITextBorderStyleNone |
placeholder | NSString | 提醒文本 | |
clearsOnBeginEditing | BOOL | 开始编辑时候清空内容 | NO |
adjustsFontSizeToFitWidth | BOOL | 以宽度主动调整字体大小 | NO |
background | UIImage | 背景 | |
clearButtonMode | UITextFieldViewMode | 设置什么时候显示革除按钮 | UITextFieldViewModeNever |
leftView | UIView | 右边视图 | |
rightView | UIView | 左边视图 | |
inputView | UIView | 响应输出时候显示的视图 | |
leftViewMode | UITextFieldViewMode | 设置什么时候显示右边视图模式 | UITextFieldViewModeNever |
rightViewMode | UITextFieldViewMode | 设置什么时候显示左边视图模式 | UITextFieldViewModeNever |
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];