初始化
UITextField *tf = [[UITextField alloc] init];
typedef NS_ENUM(NSInteger, UITextBorderStyle) {
//没有任何边框
UITextBorderStyleNone,
//线性边框
UITextBorderStyleLine,
//阴影效果边框
UITextBorderStyleBezel,
//原型效果边框
UITextBorderStyleRoundedRect
};
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
//从不显示
UITextFieldViewModeNever,
//编辑的时候显示
UITextFieldViewModeWhileEditing,
//非编辑的时候显示
UITextFieldViewModeUnlessEditing,
//任何时候都显示
UITextFieldViewModeAlways
};
//
//NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextInput, NSCoding>
//
//获取和设置文字
@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
// 文字的font
@property(nullable, nonatomic,strong) UIFont *font; // default is nil. use system font 12 pt
//对齐方式
@property(nonatomic) NSTextAlignment textAlignment; // default is NSLeftTextAlignment
//边框样式
@property(nonatomic) UITextBorderStyle borderStyle; // default is UITextBorderStyleNone. If set to UITextBorderStyleRoundedRect, custom background images are ignored.
//文字的大小,颜色等属性统一设置,影响较广泛,(全局)
@property(nonatomic,copy) NSDictionary<NSString *, id> *defaultTextAttributes NS_AVAILABLE_IOS(7_0); // applies attributes to the full range of text. Unset attributes act like default values.
//
//占位文字
@property(nullable, nonatomic,copy) NSString *placeholder; // default is nil. string is drawn 70% gray
//富文本占位文字
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder NS_AVAILABLE_IOS(6_0); // default is nil
// 编辑完一次后,再次回来编辑会清空上次编辑的所有内容(为yes时)
@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(nullable, nonatomic,weak) id<UITextFieldDelegate> delegate; // default is nil. weak reference
//背景图片(会拉伸)
@property(nullable, nonatomic,strong) UIImage *background; // default is nil. draw in border rect. image should be stretchable
//不可用时背景图片
@property(nullable, nonatomic,strong) UIImage *disabledBackground; // default is nil. ignored if background not set. image should be stretchable
//
//是否在编辑状态
@property(nonatomic,readonly,getter=isEditing) BOOL editing;
//是否允许更改字符属性字典
@property(nonatomic) BOOL allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0); // default is NO. allows editing text attributes with style operations and pasting rich text
//设置属性字典
@property(nullable, nonatomic,copy) NSDictionary<NSString *, id> *typingAttributes NS_AVAILABLE_IOS(6_0); // automatically resets when the selection changes
//
//// You can supply custom views which are displayed at the left or right
//// sides of the text field. Uses for such views could be to show an icon or
//// a button to operate on the text in the field in an application-defined
//// manner.
////
//// A very common use is to display a clear button on the right side of the
//// text field, and a standard clear button is provided.
//
//设置清除按钮的显示模式
@property(nonatomic) UITextFieldViewMode clearButtonMode; // sets when the clear button shows up. default is UITextFieldViewModeNever
//
//设置输入框左边的view
@property(nullable, nonatomic,strong) UIView *leftView; // e.g. magnifying glass
//设置输入框左视图的显示模式
@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
//
//// drawing and positioning overrides
//
- (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 (nullable, readwrite, strong) UIView *inputView;
//键盘上的bar(辅助工具条)
@property (nullable, readwrite, strong) UIView *inputAccessoryView;
//
//这个属性设置是否允许再次编辑时在内容中间插入内容
@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>
//
@optional
//
//点击输入框时触发的方法,返回YES则可以进入编辑状态,NO则不能。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
//开始编辑时调用的方法
- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
//将要结束编辑时调用的方法,返回YES则可以结束编辑状态,NO则不能
- (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
//
//输入字符时调用的方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
//
//点击清除按钮时调用的函数,返回YES则可以清除,点击NO则不能清除
- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
//点击return键触发的函数
- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
//
@end//
//NS_ASSUME_NONNULL_END
键盘代理
2016-03-18 07:54:42.866 UITextField[2479:169303] -[ViewController textFieldShouldBeginEditing:]
2016-03-18 07:54:42.880 UITextField[2479:169303] -[ViewController textFieldDidBeginEditing:]
2016-03-18 07:54:46.215 UITextField[2479:169303] -[ViewController textField:shouldChangeCharactersInRange:replacementString:]
2016-03-18 07:54:48.893 UITextField[2479:169303] -[ViewController textField:shouldChangeCharactersInRange:replacementString:]
2016-03-18 07:54:51.012 UITextField[2479:169303] -[ViewController textField:shouldChangeCharactersInRange:replacementString:]
2016-03-18 07:54:53.368 UITextField[2479:169303] -[ViewController textFieldShouldEndEditing:]
2016-03-18 07:54:53.375 UITextField[2479:169303] -[ViewController textFieldDidEndEditing:]