UITextField对象是可编辑文本框。实现了UiTextInputTraits协议,与键盘相关的设置由UiTextInputTraits中定义的属性来完成。
文本属性:
1.设置文本框显示值:
@property(nonatomic, copy) NSString *text
2.显示样式文本(用于自定义字体):
@property(nonatomic, copy) NSAttributedString *attributedText
3.文本框没有(输入,即没有内容时)文字时显示的字:
@property(nonatomic, copy) NSString *placeholder
默认值是nil。可以在文本框中显示灰色的字,用于提示用户应该在这个文本框输入什么内容。当这个文本框中输入了数据时,用于提示的灰色的字将会自动消失。
4.上面的sytle版:
@property(nonatomic, copy) NSAttributedString *attributedPlaceholder
5.默认文本属性:
@property(nonatomic, copy) NSDictionary *defaultTextAttributes
6.字体:
@property(nonatomic, retain) UIFont *font
7.文本颜色:
@property(nonatomic, retain) UIColor *textColor
8.设置文本输入框中显示文本字符串的横向位置:
@property(nonatomic) NSTextAlignment textAlignment
9.用户新输入的文本:
@property(nonatomic, copy) NSDictionary *typingAttributes
文本字段:
10.一个布尔值,指示是否应减少字体大小以适应文本字符串转换成文本域的边界矩形:
@property(nonatomic) BOOL adjustsFontSizeToFitWidth
默认值NO。设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动 。
11.允许的最小字号:
@property(nonatomic) CGFloat minimumFontSize
编辑管理:
12.是否在编辑模式:
@property(nonatomic, readonly, getter=isEditing) BOOL editing
13.开始编辑时,是否删除旧的文本字段:
@property(nonatomic) BOOL clearsOnBeginEditing
14.是否插入文本替换以前的内容:
@property(nonatomic) BOOL clearsOnInsertion
15.用户是否可以在文本字段编辑文本属性:
@property(nonatomic) BOOL allowsEditingTextAttributes
设置视图的背景外观:
16.改变文本输入框的边框外观:
@property(nonatomic) UITextBorderStyle borderStyle
typedef enum {
UITextBorderStyleNone ,//无边框线
UITextBorderStyleLine ,//仅有边框线
UITextBorderStyleBezel ,//边框线+阴影
UITextBorderStyleRoundedRect// 圆角四边形+阴影
} UITextBorderStyle;
17.设置背景图片:
@property(nonatomic, retain) UIImage *background
18.
@property(nonatomic, retain) UIImage *disabledBackground
管理覆盖:
19.设置清空按钮:
@property(nonatomic) UITextFieldViewMode clearButtonMode
20.左右侧追加UIView:
@property(nonatomic, retain) UIView *leftView
@property(nonatomic, retain) UIView *rightView
21.控制左右侧UIView状态:
@property(nonatomic) UITextFieldViewMode leftViewMode
@property(nonatomic) UITextFieldViewMode rightViewMode
重写绘制行为:
22.重写来重置文字区域:
- (CGRect)textRectForBounds:(CGRect)bounds
23.改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了:
- (void)drawTextInRect:(CGRect)rect
24.重写来重置占位符区域:
- (CGRect)placeholderRectForBounds:(CGRect)bounds
25.重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了:
- (void)drawPlaceholderInRect:(CGRect)rect
26.重写来重置边缘区域:
- (CGRect)borderRectForBounds:(CGRect)bounds
27.重写来重置编辑区域:
- (CGRect)editingRectForBounds:(CGRect)bounds
28.重写来重置clearButton位置,改变size可能导致button的图片失真:
- (CGRect)clearButtonRectForBounds:(CGRect)bounds
- (CGRect)leftViewRectForBounds:(CGRect)bounds
- (CGRect)rightViewRectForBounds:(CGRect)bounds
取代系统的输入:
30.代替系统的标准键盘:
@property(readwrite, retain) UIView *inputView
31.编辑时显示在系统键盘或用户自定义的inputView上面的视图:
@property(readwrite, retain) UIView *inputAccessoryView
通知:
UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification
UITextFieldDelegate代理:
编辑管理:
1.编辑开始前被调用:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
此时返回NO时,编辑将不会开始。
2.编辑结束前被调用:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
此时返回NO时,编辑将不会开始。
3.编辑开始后被调用:
- (void)textFieldDidBeginEditing:(UITextField *)textField
4.编辑结束后开始调用:
- (void)textFieldDidEndEditing:(UITextField *)textField
编辑文本字段中的文本:
5.文本修改前被调用:
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
range为改变范围,string中传入了修改后的字符串。此时返回NO后,修改将不会被反应。
6.文本输入框清空按钮被触摸时被调用:
- (BOOL)textFieldShouldClear:(UITextField *)textField
此时返回YES时输入框内容将被清空,NO时保持原状。
7.键盘上的回车键被触摸后调用:
- (BOOL)textFieldShouldReturn:(UITextField *)textField