OC内容输入篇(二)UITextField
2018-08-11 本文已影响68人
oceanfive
[TOC]
常用属性,参考 UILabel
@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
@property(nonatomic) NSTextAlignment textAlignment; // default is NSLeftTextAlignment
边框border
@property(nonatomic) UITextBorderStyle borderStyle;
边框样式,默认是 UITextBorderStyleNone
, 取值有:
-
UITextBorderStyleNone
: 没有边框效果 -
UITextBorderStyleLine
: 线 -
UITextBorderStyleBezel
: 斜面,感觉和 line 类似 -
UITextBorderStyleRoundedRect
: 四周是圆角
设置了
borderStyle
,background
和disabledBackground
会无效
包含rightView
,不包含leftView
整个的文字属性 defaultTextAttributes
@property(nonatomic,copy) NSDictionary<NSString *, id> *defaultTextAttributes NS_AVAILABLE_IOS(7_0);
设置的值参考 NSAttributedString
一些常用的属性比如font
、textColor
有快捷方式设置,对于一些其他的属性(比如下滑线,背景色下划线等等),可以使用defaultTextAttributes
进行设置
占位字符
@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
输入内容的属性控制
@property(nonatomic, copy) NSDictionary<NSString *,id> *typingAttributes
-
typingAttributes
: 用来控制输入的时候,输入的文字的属性,比如斜体,下划线等等;The attributes to apply to new text being entered by the user.
- 如果选中了或者选中范围发生变化,属性会被清空
nil
; - 只能在
编辑中
的状态进行属性赋值和读取,不在编辑中的状态获取到的内容为nil
;
清除内容
@property(nonatomic) BOOL clearsOnBeginEditing; // default is NO which moves cursor to location clicked. if YES, all text cleared
@property(nonatomic) UITextFieldViewMode clearButtonMode; // sets when the clear button shows up. default is UITextFieldViewModeNever
@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.
-
clearsOnBeginEditing
:-
YES
: 在开始编辑的时候就清除内容 -
NO
: 光标移动到内容的最后一位
-
-
clearButtonMode
: 清除按钮的显示模式UITextFieldViewModeNever
UITextFieldViewModeWhileEditing
UITextFieldViewModeUnlessEditing
UITextFieldViewModeAlways
自动调整
@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,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
background
和上、右、下
有间距;
当 enabled
取值:
-
YES
: 显示的是background
-
NO
: 显示的是disabledBackground
禁止编辑
一、enabled
属性值设置为 NO
二、代理方法返回NO
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
// 编辑中的判断方法
@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
-
allowsEditingTextAttributes
: 会有加粗bold
、斜体italic
、下划线underline
的操作
- (BOOL)endEditing:(BOOL)force; // use to make the view or any subview that is the first responder resign (optionally force)
- 结束编辑状态
左侧/右侧附加内容
@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
leftView和rightView.png包含了
rightView
,但是不包含leftView
代理方法
// 一、允许编辑判断 ---------------------
// 返回 YES : 可以编辑; NO: 不可以编辑,输入
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
// 二、编辑中 ---------------------
// 开始编辑,弹出了键盘
- (void)textFieldDidBeginEditing:(UITextField *)textField;
// 是否允许输入;YES:可以输入;NO:不可以输入;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
// 点击“右边”的“清除按钮”,YES:清除内容;NO:不会清除内容
- (BOOL)textFieldShouldClear:(UITextField *)textField;
// 点击 `return` 键的处理
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
// 三、结束编辑判断-------------
// 返回YES,允许结束编辑;返回NO,不允许结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
// 四、结束编辑------------
// 已经结束编辑了
- (void)textFieldDidEndEditing:(UITextField *)textField;
// 10.0 之后调用,结束编辑了
//- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0);
不点击删除键的方法调用顺序如下:
-[ThreeViewController textFieldShouldBeginEditing:]
-[ThreeViewController textFieldDidBeginEditing:]
-[ThreeViewController textField:shouldChangeCharactersInRange:replacementString:]
-[ThreeViewController textFieldShouldReturn:]
-[ThreeViewController textFieldShouldEndEditing:]
-[ThreeViewController textFieldDidEndEditing:reason:]
通知方法
UIKIT_EXTERN NSNotificationName const UITextFieldTextDidBeginEditingNotification;
UIKIT_EXTERN NSNotificationName const UITextFieldTextDidEndEditingNotification;
UIKIT_EXTERN NSNotificationName const UITextFieldTextDidChangeNotification;
UIKIT_EXTERN NSString *const UITextFieldDidEndEditingReasonKey NS_AVAILABLE_IOS(10_0);
小结:
-
defaultTextAttributes
: 是显示的文字内容的属性,(之前可能就存在内容了) -
typingAttributes
: 是输入的内容的属性 -
markedTextStyle
: 临时的,用户还没有确定输入的内容的属性