概述
UITextField
在界面中显示可编辑文本区域的对象。- 您可以使用文本字段来使用屏幕键盘从用户收集基于文本的输入。键盘可以配置许多不同类型的输入,如纯文本,电子邮件,数字等等。文本字段使用目标操作机制和委托对象来报告在编辑过程中所做的更改。
除了基本的文本编辑行为之外,还可以将叠加视图添加到文本字段以显示其他信息并提供其他可定位控件。您可以为诸如书签按钮或搜索图标等元素添加自定义叠加视图。文本字段提供内置的叠加视图来清除当前文本。自定义覆盖视图的使用是可选的。
属性和方法
初始化
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
设置占位文本
textField.placeholder = @"请输入文字";
设置文本
textField.text = @"测试";
设置文本的颜色
textField.textColor = [UIColor redColor];
设置文本的字体
textField.font = [UIFont systemFontOfSize:14];
设置文本的对齐方式
textField.textAlignment = NSTextAlignmentRight;
设置输入框不能编辑
[textField setEnabled:NO];
设置编辑框中的内容密码显示
textField.secureTextEntry = YES;
启用文本字段时显示的背景图像。该图像显示在文本字段内容的其余部分后面。
textField.background = [UIImage imageNamed:@"登录logo"];
设置边框样式(更多边框样式到补充说明中查看)默认的样式为UITextBorderStyleNone
textField.borderStyle = UITextBorderStyleRoundedRect;
设置清除按钮的模式(更多清除按钮的模式到补充说明中查看)默认样式为UITextFieldViewModeNever
textField.clearButtonMode = UITextFieldViewModeUnlessEditing;
文本字段文本的最小字体大小。当“调整为适合”选项启用时,文本字段会自动更改字体大小以确保文本的最大可读性。您可以使用此属性来指定您认为适合文本的最小字体大小。
textField.minimumFontSize = 12;
设置键盘类型(更多键盘类型到补充说明中查看)
textField.keyboardType = UIKeyboardTypeNumberPad;
设置键盘上返回键的类型(更多返回类型到补充说明中查看)
textField.returnKeyType = UIReturnKeyJoin;
设置键盘的视觉样式(更多键盘的视觉效果到补充说明中查看)
textField.keyboardAppearance = UIKeyboardAppearanceLight;
文本字段的拼写检查行为。此属性决定了拼写检查在打字过程中是启用还是禁用
textField.spellCheckingType = UITextSpellCheckingTypeNo;
此属性决定了拼写检查在打字过程中是启用还是禁用。启用拼写检查后,文本对象会为所有拼写错误的单词生成红色下划线。如果用户点击拼写错误的单词,则文本对象向用户呈现可能的更正列表。
此属性的默认值是default,启用自动更正时启用拼写检查。此属性中的值将覆盖用户在“设置”>“常规”>“键盘”中设置的拼写检查设置。
文本字段的自动纠正行为。此属性确定在输入过程中自动更正是启用还是禁用
textField.autocorrectionType = UITextAutocorrectionTypeYes;
自动大写样式适用于键入的文本。此属性决定在什么时候自动按下Shift
键
textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
设置左边视图(注意:需要先设置左边视图的显示模式为UITextFieldViewModeAlways
)
textField.leftViewMode = UITextFieldViewModeAlways; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)]; imageView.image = [UIImage imageNamed:@"验证码"]; textField.leftView = imageView;
设置右边视图(注意:需要先设置右边视图的显示模式为UITextFieldViewModeAlways
)
textField.rightViewMode = UITextFieldViewModeAlways; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)]; imageView.image = [UIImage imageNamed:@"验证码"]; textField.rightView = imageView;
代理方法
询问委托人是否应该在指定的文本字段中开始编辑。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
告诉委托人在指定的文本字段中开始编辑。
- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
询问委托人是否应在指定的文本字段中停止编辑。
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
告诉委托人对指定的文本字段停止编辑。
- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
告诉委托人对指定的文本字段停止编辑。
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReaso