一、UITextField 关系图
二、UITextField 属性
@property(nullable,nonatomic,copy) NSString * text;
2. 保存 UITextField 对象的文本颜色;默认为 nil,使用黑色
@property(nullable,nonatomic,strong)UIColor * textColor;
3. 保存 UITextField 对象的文本字体大小;默认为 nil,使用 12 号
@property(nullable,nonatomic,strong)UIFont * font;
4. 保存 UITextField 对象的文本对齐方式;默认为 NSLeftTextAlignment
@property(nonatomic) NSTextAlignment textAlignment;
// NSTextAlignment 枚举
typedef NS_ENUM(NSInteger, NSTextAlignment) {
NSTextAlignmentLeft = 0, // 左对齐
NSTextAlignmentCenter = 1, // 右对齐
NSTextAlignmentRight = 2, // 居中
NSTextAlignmentJustified = 3, // 在一个段落的最后一行自然对齐
NSTextAlignmentNatural = 4, // 默认的对齐
}
5. 保存 UITextField 对象的边框样式;默认为 UITextBordeStyleNone
@property(nonatomic) UITextBorderStyle borderStyle;
//UITextFiledStyle 枚举
typedef NS_ENUM(NSInteger, UITextBorderStyle) {
UITextBorderStyleNone, //无边框
UITextBorderStyleLine, //黑色线边框
UITextBorderStyleBezel, //有边框和阴影
UITextBorderStyleRoundedRect // 圆角矩形
};
6. 保存 UITextField 对象的占位符文本;默认为空,且字符是亮灰色
@property(nullable,nonatomic,copy) NSString * placeholder;
7. 再次编辑 UITextField 对象是否清空当前的内容;默认为 NO
@property(nonatomic) BOOL clearsOnBeginEditing;
8. 设置字体大小是否随边框宽度自适应;默认为 NO
当字体大小过大以至于整个边框不能够全部显示时,会随着 minimumFontSize 的值减少,当已经填满整个边框则不会再减少
@property(nonatomic) BOOL adjustsFontSizeToFitWidth;
9. 保存 UITextField 对象的最小字体;默认为 0
@property(nonatomic) CGFloat minimumFontSize;
10. 设置 UITextField 对象的背景图片;默认为空,会拉伸图片
@property(nullable,nonatomic,strong)UIImage * background;
11. 设置 UITextField 对象不可用时的背景图片;默认为空,如果没有设置 background,则忽视
@property(nullable,nonatomic,strong)UIImage * disabledBackground;
12. 保存 UITextField 对象是否处于编辑
@property(nonatomic,readonly,getter=isEditing)BOOL editing;
13. 设置清除按钮的样式,就是文本框右面的叉叉;默认 UITextFieldViewModeNever
@property(nonatomic) UITextFieldViewMode clearButtonMode;
//UITextFieldViewMode 枚举
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever, // 从不显示
UITextFieldViewModeWhileEditing, //处于编辑模式显示
UITextFieldViewModeUnlessEditing, //非编辑模式显示
UITextFieldViewModeAlways // 总是显示
};
14. 设置 UITextField 对象的左视图,
@property(nullable,nonatomic,strong)UIView * leftView;
15. 设置 UITextField 对象的左视图的显示模式;默认是 UITextFieldViewModeNever
@property(nonatomic) UITextFieldViewMode leftViewMode;
16. 设置 UITextField 对象的右视图
@property(nullable,nonatomic,strong)UIView * rightView;
17. 设置 UITextField 对象的右视图的显示模式;默认是 UITextFieldViewModeNever
@property(nonatomic) UITextFieldViewMode rightViewMode;
左视图与右视图中间就是文本区域
18. 当 UITextField 对象称为第一响应者时,弹出的视图(类似于键盘)
@property (nullable,readwrite,strong)UIView *inputView;
19. 当 UITextField 对象称为第一响应者时,弹出的辅助视图(类似于键盘)
@property (nullable,readwrite,strong)UIView *inputAccessoryView;
20. 设置是否允许再次编辑时在内容中间插入内容;默认为 NO,即只能子啊头尾插入新的内容
@property(nonatomic)BOOL clearsOnInsertion;
21. UITextField 的委托
@property(nullable,nonatomic,weak) id<UITextFieldDelegate> delegate;
三、UITextField方法
1. 重绘方法
- (CGRect)borderRectForBounds:(CGRect)bounds;
②重置文字区域
- (CGRect)textRectForBounds:(CGRect)bounds;
③重置占位符区域
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
④重置编辑区域
- (CGRect)editingRectForBounds:(CGRect)bounds;
⑤重置 clearButton 的区域
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
⑥重置 leftView 的区域
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
⑦重置 rightView 的区域
- (CGRect)rightViewRectForBounds:(CGRect)bounds;
⑧重写 UITextField 对象的文本的方法
- (void)drawTextInRect:(CGRect)rect;
⑨重写 UITextField 对象的占位符文本的方法
- (void)drawTextInRect:(CGRect)rect;
2. UIView(UITextField)
- (BOOL)endEditing:(BOOL)force;
3. UITextFieldDelegate 协议方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
②获得焦点时触发该方法,成为第一响应者
- (void)textFieldDidBeginEditing:(UITextField *)textField;
③结束编辑时触发该方法,返回 YES 允许停止编辑或注销第一响应者,否则 不允许编辑结束
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
④失去第一响应者时触发该方法
- (void)textFieldDidEndEditing:(UITextField *)textField;
⑥询问 文本是否可以被替换,如果返回 YES 则可被替换,否则不能被替换
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
⑦点击清除按钮时触发该方法,返回 YES 可以清除,否则不能清除
- (BOOL)textFieldShouldClear:(UITextField *)textField;
⑧点击返回按钮时触发该方法,返回 YES 可以返回,否则不能返回
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
四、UITextInputTraits 协议
1. 设置键盘的风格样式,默认是UIKeyboardTypeDefault
@property(nonatomic) UIKeyboardType keyboardType;
typedef NS_ENUM(NSInteger, UIKeyboardType) {
UIKeyboardTypeDefault, // 默认键盘样式
UIKeyboardTypeASCIICapable, // 输入字母的键盘
UIKeyboardTypeNumbersAndPunctuation, //输入数字符号键盘
UIKeyboardTypeURL, //输入 URL 键盘
UIKeyboardTypeNumberPad, //输入数字键盘(九键)
UIKeyboardTypePhonePad, //输入电话号码键盘
UIKeyboardTypeNamePhonePad, //输入人名和电话号码键盘
UIKeyboardTypeEmailAddress, //输入邮件地址键盘
UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1), // 输入数字和小数点的键盘
UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0), // 容易输入 @ 和 # 的键盘
UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0), // 搜索网页的键盘
UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0), // A number pad (0-9) that will always be ASCII digits.
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // 废弃
};
2. 设置键盘的返回键类型;默认是 UIReturnKeyTypeDefault
@property(nonatomic) UIReturnKeyType returnKeyType;
typedef NS_ENUM(NSInteger, UIReturnKeyType) {
UIReturnKeyDefault, // 显示 return
UIReturnKeyGo, // 显示 Go
UIReturnKeyGoogle, // 显示 Search
UIReturnKeyJoin, // 显示 Join
UIReturnKeyNext, // 显示 Next
UIReturnKeyRoute, // 显示 Route
UIReturnKeySearch, // 显示 Search
UIReturnKeySend, // 显示 Send
UIReturnKeyYahoo, // 显示 Search
UIReturnKeyDone, // 显示 Done
UIReturnKeyEmergencyCall, // 显示 EmergencyCall
UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0), // 显示 Continue
};
3. 设置是否自动大写
@property(nonatomic) UITextAutocapitalizationType autocapitalizationType;
typedef NS_ENUM(NSInteger, UITextAutocapitalizationType) {
UITextAutocapitalizationTypeNone, // 不能自动有效
UITextAutocapitalizationTypeWords, // 单词开头的情况下自动有效,在输入单词时第一个最为大学,剩下的字母为小写
UITextAutocapitalizationTypeSentences, // 文章开头的情况下自动有效,在输入很长的文章的时,第一个单词的第一个字母为大写,其余均为小写
UITextAutocapitalizationTypeAllCharacters, // 一值自动有效,一直是大写
};
@property(nonatomic) UITextAutocorrectionType autocorrectionType;
typedef NS_ENUM(NSInteger, UITextAutocorrectionType) {
UITextAutocorrectionTypeDefault, // 默认
UITextAutocorrectionTypeNo, // 禁止自动矫正
UITextAutocorrectionTypeYes, // 开启自动矫正
};
@property(nonatomic) UIKeyboardAppearance keyboardAppearance;
typedef NS_ENUM(NSInteger, UIKeyboardAppearance) {
UIKeyboardAppearanceDefault,// 默认外观,浅灰色
UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0), // 深灰色
UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0), // 浅灰色
UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark, // 废弃
};
6. 设置是否安全输入;默认是 NO
@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry;