typedef NS_ENUM(NSInteger, UITextBorderStyle) {
UITextBorderStyleNone, //没有边线框
UITextBorderStyleLine, //方框
UITextBorderStyleBezel, //略像winXP的输入框
UITextBorderStyleRoundedRect //圆角矩形,此风格设置背景图片无效
};
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever, //从不
UITextFieldViewModeWhileEditing, //当编辑的时候有效
UITextFieldViewModeUnlessEditing, //除了编辑的时候,其他时候有效
UITextFieldViewModeAlways //总是有效
};
@interface UITextField : UIControl <UITextInput, NSCoding>
@property(nonatomic,copy) NSString *text; //textField中的文本 default is nil
@property(nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); // default is nil
@property(nonatomic,retain) UIColor *textColor; //textField中文本的颜色 default is nil. use opaque black
@property(nonatomic,retain) UIFont *font; //textField中文本的格式 default is nil. use system font 12 pt
@property(nonatomic) NSTextAlignment textAlignment; //textField中文本的对齐方式 default is NSLeftTextAlignment
@property(nonatomic) UITextBorderStyle borderStyle; //textField的边框风格 default is UITextBorderStyleNone. If set to UITextBorderStyleRoundedRect, custom background images are ignored.
@property(nonatomic,copy) NSDictionary *defaultTextAttributes NS_AVAILABLE_IOS(7_0); // applies attributes to the full range of text. Unset attributes act like default values.
@property(nonatomic,copy) NSString *placeholder; //textField为空时,默认的灰色提示文本 default is nil. string is drawn 70% gray
@property(nonatomic,copy) NSAttributedString *attributedPlaceholder NS_AVAILABLE_IOS(6_0); // default is nil
@property(nonatomic) BOOL clearsOnBeginEditing; //开始输入时清空原有输入内容default is NO which moves cursor to location clicked. if YES, all text cleared
@property(nonatomic) BOOL adjustsFontSizeToFitWidth; //用处不大,当文本很长时,文本适应输入框宽度而缩小 default is NO. if YES, text will shrink to minFontSize along baseline
@property(nonatomic) CGFloat minimumFontSize; //用处不大 default is 0.0. actual min may be pinned to something readable. used if adjustsFontSizeToFitWidth is YES
@property(nonatomic,assign) id<UITextFieldDelegate> delegate; // default is nil. weak reference
@property(nonatomic,retain) UIImage *background; //输入框enable = YES的时候(需要边框类型设置为UITextBorderStyleNone)default is nil. draw in border rect. image should be stretchable
@property(nonatomic,retain) UIImage *disabledBackground; //输入框的enable = NO时显示的图片(需要设置background) default is nil. ignored if background not set. image should be stretchable
@property(nonatomic,readonly,getter=isEditing) BOOL editing; //获取当前textField是否处于编辑状态
@property(nonatomic) BOOL allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0); // default is NO. allows editing text attributes with style operations and pasting rich text
@property(nonatomic,copy) NSDictionary *typingAttributes NS_AVAILABLE_IOS(6_0); // automatically resets when the selection changes
@property(nonatomic) UITextFieldViewMode clearButtonMode; //设置右侧清空按钮显示的时间场合 sets when the clear button shows up. default is UITextFieldViewModeNever
@property(nonatomic,retain) UIView *leftView; // e.g. magnifying glass
@property(nonatomic) UITextFieldViewMode leftViewMode; //设置什么状态时显示左图片 sets when the left view shows up. default is UITextFieldViewModeNever
@property(nonatomic,retain) UIView *rightView; // e.g. bookmarks button
@property(nonatomic) UITextFieldViewMode rightViewMode; //设置什么状态时显示右图片 sets when the right view shows up. default is UITextFieldViewModeNever
// drawing and positioning overrides
//这些方法用于自定制时进行重写方法,以实现绘制和放置自定制的textField的不同的属性的位置。这些函数的返回值就相当于一些参数,用于系统内部在实现生成控件时调用。
- (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;
- (void)drawTextInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect;
// Presented when object becomes first responder. If set to nil, reverts to following responder chain. If
// set while first responder, will not take effect until reloadInputViews is called.
@property (readwrite, retain) UIView *inputView; //可以用View替换点击TextField弹出的键盘
@property (readwrite, retain) UIView *inputAccessoryView; //键盘上方会出现一个View作为输入的辅助view
@property(nonatomic) BOOL clearsOnInsertion NS_AVAILABLE_IOS(6_0); // defaults to NO. if YES, the selection UI is hidden, and inserting text will replace the contents of the field. changing the selection will automatically set this to NO.
@end
@interface UIView (UITextField)
- (BOOL)endEditing:(BOOL)force; //可以用于实现退出编辑,比如添加到触摸或手势中 use to make the view or any subview that is the first responder resign (optionally force)
@end
@protocol UITextFieldDelegate <NSObject> //提供用户在文本框不同状态时自定义执行的方法
UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;