UITextField各个属性
@property(nullable, nonatomic,copy) NSString *text; // default is nil
// 普通的显示的文字
@property(nullable, nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); // default is nil
// 可显示的富文本文字
@property(nullable, nonatomic,strong) UIColor *textColor; // default is nil. use opaque black
// 默认字体的颜色为黑色
@property(nullable, nonatomic,strong) UIFont *font; // default is nil. use system font 12 pt
// 默认的字体大小 12pt
@property(nonatomic) NSTextAlignment textAlignment; // default is NSLeftTextAlignment
// 默认的段落的对齐方式 居左,居中,居右;
@property(nonatomic) UITextBorderStyle borderStyle;
// 文本框的外框样式
@property(nonatomic,copy) NSDictionary<NSString *, id> *defaultTextAttributes NS_AVAILABLE_IOS(7_0);
// 默认的富文本的样式, 所有的文本.
@property(nullable, nonatomic,copy) NSString *placeholder; // default is nil. string is drawn 70% gray
// 默认的占位文字, 文字颜色为 70%灰;
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder NS_AVAILABLE_IOS(6_0);
// 用富文本时, 默认的占位文字
@property(nonatomic) BOOL clearsOnBeginEditing;
// 是否在重新编辑的时候清除文本
@property(nonatomic) BOOL adjustsFontSizeToFitWidth; // default is NO. if YES, text will shrink to minFontSize along baseline
// 根据字体的大小, 当文本长度超过textField的长度的时候, 为了全部显示, 自动缩小字体.
@property(nonatomic) CGFloat minimumFontSize; // default is 0.0. actual min may be pinned to something readable. used if adjustsFontSizeToFitWidth is YES
// 设置最小适应的字体, 和adjustsFontSizeToFitWidth配合使用.
@property(nullable, nonatomic,strong) UIImage *background;
// 背景图片
@property(nonatomic) UITextFieldViewMode clearButtonMode;
// 清除按钮的显示模式
@property(nullable, nonatomic,strong) UIView *leftView;
// 左侧视图,
@property(nonatomic) UITextFieldViewMode leftViewMode; // sets when the left view shows up. default is UITextFieldViewModeNever
// 左侧显示视图模式
@property(nullable, nonatomic,strong) UIView *rightView; // e.g. bookmarks button
// 右侧视图, 设置后可能会挡住 清理按钮.
@property(nonatomic) UITextFieldViewMode rightViewMode; // sets when the right view shows up. default is UITextFieldViewModeNever
UITextField 各个方法
// 以下方法在继承于UITextfield使用, 自定义这些区域.
- (CGRect)textRectForBounds:(CGRect)bounds; // 重绘文本的显示区域
- (CGRect)placeholderRectForBounds:(CGRect)bounds; //重绘placeholder的显示区域
- (CGRect)editingRectForBounds:(CGRect)bounds; // 重绘编辑的区域
- (CGRect)clearButtonRectForBounds:(CGRect)bounds; // 重绘clearButton的区域
- (CGRect)leftViewRectForBounds:(CGRect)bounds; // 重左侧视图的区域
- (CGRect)rightViewRectForBounds:(CGRect)bounds; // 重绘右侧视图的区域
UITextField的代理方法 "UITextFieldDelegate"
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
// 是否允许编辑, 在开始编辑之前调用 若返回No则TextField不可编辑
- (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
// 结束编辑, 当前textFeild不再是第一响应了, 调用此方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
// 编辑的时候, text变化,调用此方法
- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
// 是否对清理按钮做出反应
- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
// 是否对键盘上的return键做响应
代理方的打印顺序如下图:
- 获得通知
通过监听这三个动作, 可以做出相应的处理.
UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;