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, 取值有:

设置了 borderStylebackgrounddisabledBackground会无效
包含rightView,不包含leftView

整个的文字属性 defaultTextAttributes

@property(nonatomic,copy)   NSDictionary<NSString *, id>           *defaultTextAttributes NS_AVAILABLE_IOS(7_0);

设置的值参考 NSAttributedString

一些常用的属性比如fonttextColor有快捷方式设置,对于一些其他的属性(比如下滑线,背景色下划线等等),可以使用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

清除内容

@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.

自动调整

@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 取值:

UITextField_background.png

禁止编辑

一、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

- (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

包含了rightView,但是不包含leftView

leftView和rightView.png

代理方法

// 一、允许编辑判断 ---------------------
// 返回 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);

小结:

最后

GitHub参考链接

上一篇下一篇

猜你喜欢

热点阅读