1. enablesReturnKeyAutomatically
默认为No,如果设置为Yes,文本框中没有输入任何字符的话,右下角的返回按钮是disabled的。
3.backgroundColor
设置输入框的背景颜色,此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉
设置UITextField 的enable属性为no时,UITextField 的背景
5.placeholder
当输入框为空时, 显示提示内容xxxxxxxx
9. text
默认在输入框中显示的文字
12. clearsOnBeginEditing
设置是否再次编辑就清空
16. keyboardType
设置键盘的样式
设置键盘外观
20. delegate
设置代理 用于实现协议
21. rightView
最右侧加图片是以下代码 左侧类似
22. editing
是否允许编辑(只读属性)。
23.调整键盘
inputView //代替标准的系统键盘
inputAccessoryView //编辑时显示在系统键盘或用户自定义的inputView上面的视图
24. delegate
类要遵守UITextFieldDelegate协议
25.通知
UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件
UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification
当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。
因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知
UIKeyboardWillShowNotification //键盘显示之前发送
UIKeyboardDidShowNotification //键盘显示之后发送
UIKeyboardWillHideNotification //键盘隐藏之前发送
UIKeyboardDidHideNotification //键盘隐藏之后发送
默认为No,如果设置为Yes,文本框中没有输入任何字符的话,右下角的返回按钮是disabled的。
2.borderStyle
设置边框样式,只有设置了才会显示边框样式 (IOS7以后默认样式为UITextBorderStyleRoundedRect)myTextField.borderStyle = UITextBorderStyleRoundedRect;
typedef enum {
UITextBorderStyleNone,UITextBorderStyleLine,UITextBorderStyleBezel,UITextBorderStyleRoundedRect
} UITextBorderStyle;
3.backgroundColor
设置输入框的背景颜色,此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉
myTextField.backgroundColor = [UIColor whiteColor];
4.background
设置背景:myTextField.background = [UIImage imageNamed:@"backg.png"]; //borderStyle = UITextBorderStyleNone的时候改属性有效
设置UITextField 的enable属性为no时,UITextField 的背景
myTextField.disabledBackground = [UIImage imageNamed:@"xxxx.png"];
5.placeholder
当输入框为空时, 显示提示内容xxxxxxxx
myTextField.placeholder = @"xxxxxxxx";
6.font
设置输入框内容的字体样式和大小myTextField.font = [UIFont fontWithName:@"Arial" size:20.0f];
7. textColor
设置字体颜色myTextField.textColor = [UIColor redColor];
8. clearButtonMode
输入框中显示一个小叉号,用于一次性删除输入框中的内容myTextField.clearButtonMode = UITextFieldViewModeAlways;
typedef enum {
UITextFieldViewModeNever, //重不出现UITextFieldViewModeWhileEditing, //编辑时出现UITextFieldViewModeUnlessEditing, //除了编辑外都出现UITextFieldViewModeAlways //一直出现
} UITextFieldViewMode;
9. text
默认在输入框中显示的文字
yjmTextField.text = @"xxxxxxxxxx";
10. secureTextEntry
输入密码时,设置这个属性值为YES。yjmTextField.secureTextEntry = YES;
11. autocorrectionType
是否纠错myTextField.autocorrectionType = UITextAutocorrectionTypeNo;
typedef enum {
UITextAutocorrectionTypeDefault, //默认UITextAutocorrectionTypeNo, //不自动纠错UITextAutocorrectionTypeYes, //自动纠错
} UITextAutocorrectionType;
12. clearsOnBeginEditing
设置是否再次编辑就清空
myTextField.clearsOnBeginEditing = YES;
13. textAlignment
设置内容对齐方式myTextField.textAlignment = UITextAlignmentLeft;
14. contentVerticalAlignment
设置内容的垂直对齐方式 UITextField继承自UIControl,此类中有一个属性contentVerticalAlignmentmyTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
15. adjustsFontSizeToFitWidth
设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动myTextField.adjustsFontSizeToFitWidth = YES;//设置自动缩小显示的最小字体大小
myTextField. minimumScaleFactor = 20;// 当显示文本可以自动调整字体大小时,可调整的最小字体是多大
16. keyboardType
设置键盘的样式
myTextField.keyboardType = UIKeyboardTypeNumberPad;
typedef enum {
UIKeyboardTypeDefault, 默认键盘,支持所有字符UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符UIKeyboardTypeNumberPad, 数字键盘UIKeyboardTypePhonePad, 电话键盘UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符UIKeyboardTypeWebSearch 一种面向URL添加默认键盘式(显示空格 . 前往)UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;
17.autocapitalizationType
设置首字母是否大写yjmTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
typedef enum {
UITextAutocapitalizationTypeNone, 不自动大写UITextAutocapitalizationTypeWords, 单词首字母大写UITextAutocapitalizationTypeSentences, 句子的首字母大写UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
} UITextAutocapitalizationType;
18. returnKeyType
设置return键类型myTextField.returnKeyType =UIReturnKeyDone;
typedef enum {
UIReturnKeyDefault, 默认 灰色按钮,标有ReturnUIReturnKeyGo, 标有Go的蓝色按钮UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索UIReturnKeyJoin,标有Join的蓝色按钮UIReturnKeyNext,标有Next的蓝色按钮UIReturnKeyRoute,标有Route的蓝色按钮UIReturnKeySearch,标有Search的蓝色按钮UIReturnKeySend,标有Send的蓝色按钮UIReturnKeyYahoo,标有Yahoo的蓝色按钮UIReturnKeyDone,标有Yahoo的蓝色按钮UIReturnKeyEmergencyCall, 紧急呼叫按钮
} UIReturnKeyType;19. keyboardAppearance
设置键盘外观
myTextField.keyboardAppearance=UIKeyboardAppearanceDefault;
typedef enum {
UIKeyboardAppearanceDefault, //默认
UIKeyboardAppearanceDark, //暗色
UIKeyboardAppearanceLight, //亮色
UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark, // Deprecated
}UIKeyboardAppearance;
20. delegate
设置代理 用于实现协议
myTextField.delegate = self;
21. rightView
最右侧加图片是以下代码 左侧类似
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
myTextField.rightView=image;
myTextField.rightViewMode = UITextFieldViewModeAlways;
typedef enum {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
} UITextFieldViewMode;
22. editing
是否允许编辑(只读属性)。
23.调整键盘
inputView //代替标准的系统键盘
inputAccessoryView //编辑时显示在系统键盘或用户自定义的inputView上面的视图
24. delegate
类要遵守UITextFieldDelegate协议
myTextField.delegate = self; //声明myTextField的代理是控制器自己,
代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//返回一个BOOL值,YES代表允许编辑,NO不允许编辑.
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//开始编辑时触发,文本字段将成为first responder
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder
//要想在用户结束编辑时阻止文本字段消失,可以返回NO ,返回NO,点击键盘的返回按钮会无效果。
//这对一些文本字段必须始终保持活跃状态的程序很有用,比如即时消息
return NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField;{
// textFieldShouldEndEditing返回YES后执行;textFieldShouldEndEditing返回NO时有可能强制执行(e.g. view removed from window)
}
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。
//这对于想要加入撤销选项的应用程序特别有用
//可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。
//要防止文字被改变可以返回NO
//这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
//返回一个BOOL值指明是否允许根据用户请求清除内容
//可以设置在特定条件下才允许清除内容
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
//返回一个BOOL值,指明是否允许在按下回车键时结束编辑
//如果允许要调用resignFirstResponder 方法,这回导致结束编辑,而键盘会被收起[textField resignFirstResponder];
//查一下resign这个单词的意思就明白这个方法了
return YES;
}
25.通知
UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件
UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification
当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。
因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知
UIKeyboardWillShowNotification //键盘显示之前发送
UIKeyboardDidShowNotification //键盘显示之后发送
UIKeyboardWillHideNotification //键盘隐藏之前发送
UIKeyboardDidHideNotification //键盘隐藏之后发送