对比UITextField
和UITextView
类继承关系
- NSObject
- UIResponder
- UIView
- UIControl
- UITextField
- UIScrollView
- UITextView
代理方法
一
-
UITextField
:- 没有输入内容发生变化的代理方法,需要监听通知
UITextFieldTextDidChangeNotification
- 有
return
的代理方法:- (BOOL)textFieldShouldReturn:(UITextField *)textField;
- 没有输入内容发生变化的代理方法,需要监听通知
-
UITextView
:-
输入内容发生变化的代理方法
- (void)textViewDidChange:(UITextView *)textView;
-
没有
return
的代理方法,需要自己处理- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } return YES; }
-
二、下面的这些是UITextView
有但是UITextField
没有的代理方法
主要集中在:
- 选中的内容变化
-
URL
的处理 -
Attachment
的处理
// 输入了内容的:这个是`UITextField`没有的,需要使用通知处理
- (void)textViewDidChange:(UITextView *)textView;
// 选中的内容改变:这个是`UITextField`没有的
- (void)textViewDidChangeSelection:(UITextView *)textView;
// 链接 url 的处理:这个是`UITextField`没有的
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0);
// Attachment 的处理:这个是`UITextField`没有的
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0);
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_DEPRECATED_IOS(7_0, 10_0, "Use textView:shouldInteractWithURL:inRange:forInteractionType: instead");
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_DEPRECATED_IOS(7_0, 10_0, "Use textView:shouldInteractWithTextAttachment:inRange:forInteractionType: instead");
三、下面的这些是UITextField
有但是UITextView
没有的代理方法
主要集中在:
-
return
键的点击 - 右侧
clearButton
的点击
- (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.
相同的属性
@property(null_resettable,nonatomic,copy) NSString *text;
@property(nullable,nonatomic,strong) UIFont *font;
@property(nullable,nonatomic,strong) UIColor *textColor;
@property(nonatomic) NSTextAlignment textAlignment; // default is NSLeftTextAlignment
@property(null_resettable,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0);
@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 allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0);
@property(nonatomic,copy) NSDictionary<NSString *, id> *typingAttributes NS_AVAILABLE_IOS(6_0); // automatically resets when the selection changes
-
allowsEditingTextAttributes
: 会有加粗bold
、斜体italic
、下划线underline
的操作 -
typingAttributes
: 用来控制输入的时候,输入的文字的属性,比如斜体,下划线等等;The attributes to apply to new text being entered by the user.
不同点
UITextField
有但是UITextView
没有的
// 是显示的文字内容的属性,(之前可能就存在内容了)
@property(nonatomic,copy) NSDictionary<NSString *, id> *defaultTextAttributes NS_AVAILABLE_IOS(7_0);
// 边框样式
@property(nonatomic) UITextBorderStyle borderStyle;
// 占位符
@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) 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
// 自动调整,因为 UITextField 只能单行输入,所以有了这个
@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
// 正在编辑中的判断
@property(nonatomic,readonly,getter=isEditing) BOOL editing;
// 左侧/右侧 附件内容
@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
UITextView
有但是UITextField
没有的
// 选中的内容范围
@property(nonatomic) NSRange selectedRange;
// 是否可以选中
@property(nonatomic,getter=isSelectable) BOOL selectable NS_AVAILABLE_IOS(7_0);
// 自动检测数据类型
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
// 滚动到 range 范围,UITextView 是多行显示,可以滚动的
- (void)scrollRangeToVisible:(NSRange)range;
// 文字内边距,可以调整文字的输入范围
@property(nonatomic, assign) UIEdgeInsets textContainerInset NS_AVAILABLE_IOS(7_0);
// 链接url的属性设置,特殊处理
@property(null_resettable, nonatomic, copy) NSDictionary<NSString *, id> *linkTextAttributes NS_AVAILABLE_IOS(7_0);
1、选中可以通过都遵守的协议
UITextInput
获取
2、UITextView
可以自动检测一些数据类型,比如URL
UITextView
和UITextField
都有,但是处理不同的
禁止编辑:
-
UITextField
:enabled
-
UITextView
:editable
小结:
-
UITextField
: 适合单行输入,右侧有清除按钮,安全输入 -
UITextView
: 适合多上输入