苹果官方对UITextField的解释为:A UITextField object is a control that displays editable text and sends an action message to a target object when the user presses the return button. You typically use this class to gather small amounts of text from the user and perform some immediate action, such as a search operation, based on that text.
大概意思为:UITextField是一个编辑文本和操作用户信息并发送给目标的对象。UITextField一般是用于收集简单的文本资料的和处理立即要操作的数据。(UITextField的父类是UIControl,UIControl的父类是UIView)
UITextField属性:
text :本文 (默认是空的)
attributedText: 富文本 (ios6加入的一个属性)
textColor : 字体颜色 (取值为UIColor,默认字体颜色是黑色)
font:字体大小 (取值是UIFont,默认是使用12 pt)
textAlignment:文字对齐方式 (默认是向左对齐,一共有三中对齐方式: NSTextAlignmentLeft 居左,NSTextAlignmentCenter 居中,NSTextAlignmentRight 居右)
borderStyle:边界风格 (默认没有边框风格UITextBorderStyleNone,UITextBorderStyleLine :有边框 ,UITextBorderStyleBezel:阴影边框,UITextBorderStyleRoundedRect,圆角边框)
defaultTextAttributes:默认字体的属性(设置这个属性之后会对所有的字体产生影响,iOS7加入)
placeholder:默认提示
attributedPlaceholder:默认提示字体的富文本形式(iOS6加入)
clearsOnBeginEditing:编辑开始时清空输入框里面的内容(默认是no不清空)
adjustsFontSizeToFitWidth:允许字体根据文本长度自己改变字体的大小(默认是不允许)
minimumFontSize:设置自适应最小字体(一般与adjustsFontSizeToFitWidth连用)
background:背景 (取值是UIImage,可被拉升)
disabledBackground:禁用时的背景图片
editing:是否正在编辑 (是一个只读属性)
allowsEditingTextAttributes:是否能使用富文本
typingAttributes:设置富文本字典的属性
clearButtonMode:清除按钮的显示模式 (UITextFieldViewModeNever:不显示,UITextFieldViewModeWhileEditing:编辑时显示,UITextFieldViewModeUnlessEditing:不编辑时显示,UITextFieldViewModeAlways:总是显示)
leftView:UITextField的左视图
leftViewMode:左视图显示模式 (默认不显示:UITextFieldViewModeNever,UITextFieldViewModeWhileEditing:编辑的时候显示,UITextFieldViewModeUnlessEditing:不编辑的时候显示,UITextFieldViewModeAlways:总是显示)
rightView:右视图
rightViewMode:右视图显示模式(和左视图显示模式一样)
inputView:输入视图 (一个类似于键盘的视图,在textfield成为第一响应者的时候出现。不以键盘同时出现)
inputAccessoryView:辅助输入视图 (单textfield成为第一响应者的时候出现,能够和键盘同时出现,在键盘的上方)
clearsOnInsertion:是否允许再次插入输入的时候改变中间的文本内容(默认是NO)
方法
1.位置绘制
- (CGRect)borderRectForBounds:(CGRect)bounds;重置边缘区域
- (CGRect)textRectForBounds:(CGRect)bounds;重置文字区域
- (CGRect)placeholderRectForBounds:(CGRect)bounds;重置默认提示文本区域
- (CGRect)editingRectForBounds:(CGRect)bounds;重置编辑区域
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;重置删除按钮的位置
- (CGRect)leftViewRectForBounds:(CGRect)bounds;重置左视图的位置
- (CGRect)rightViewRectForBounds:(CGRect)bounds;重置右视图的位置
2.文本属性
- (void)drawTextInRect:(CGRect)rect;改变输入文本的属性(使用这个方法之后不在继承super的属性)
- (void)drawPlaceholderInRect:(CGRect)rect;改变默认提示字符的属性
方法1和2的配合使用可以重新绘制出适合自己的TextField
3.编辑状态
- (BOOL)endEditing:(BOOL)force; 结束编辑,取消第一响应者身份键盘消失
4.代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; 是否进入编辑状态(返回NO不能进入编辑状态,返回YES可以进入编辑状态成为第一响应者)
- (void)textFieldDidBeginEditing:(UITextField *)textField; 进入编辑状态成为第一响应者调用的方法
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;是否能够取消编辑状态(返回YES允许退出编辑状态,取消第一响应者。返回NO不能取消响应状态)
- (void)textFieldDidEndEditing:(UITextField *)textField; 退出编辑状态,取消第一响应者调用的方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;输入任何字符调用的方法(返回YES输入的字符有效,返回NO输入的字符无效)
- (BOOL)textFieldShouldClear:(UITextField *)textField;点击取消按钮是否有效几是否允许删除(返回YES有效,返回NO无效)
- (BOOL)textFieldShouldReturn:(UITextField *)textField;是否在点击ruturn之后结束编辑状态(YES是,NO否)
5.通知
UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification; 当文本字段开始编辑模式时触发
UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;当文本字段正在编辑时触发
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;当文本字段退出编辑模式时触