OC_UITextField
UITextField 是一个能够在界面上提供可编辑文本区域的对象。
UITextField概述
我们可以使用 UITextField
来接收用户的输入,并且可以配置对应的输入键盘,例如允许英文输入、数字输入等。并且可以使用代理的方式对输入信息进行观测和更改。 另外在 UITextFiled
中还内置了可选的按钮,例如清除按钮等,我们可以在不同的情况下选择开启。
关于键盘的显示和隐藏
当用户使用 UITextField
输入信息时,系统会自动调用 becomeFirstResponder
方法,让正在输入信息的 UITextField
对象成为第一响应者,同时我们也可以使用方法 resignFirstResponder
来注销其第一响应。
键盘的显示和隐藏会影响 UITextField
对象的输入状态。当键盘显示的时候,UITextField
对象进入编辑状态,同时开始向委托发送适当的通知,同时在键盘隐藏的时候,UITextField
对象会退出编辑状态,并通知委托。
关于键盘的外观和行为
UITextField
实现了 UITextInputTraits
的协议属性,可以对使用 UITextField
对象,修改 UITextInputTraits
的属性。我们可以根据这个来自定义键盘的样式和行为。 具体属性请看 UITextInputTraits
的解释文章。
键盘的响应通知
在键盘出现的时候会遮掩一部分屏幕,可能会将输入框挡住,在这种情况下我们可以使用通知来进行调整,使输入框能够呈现在键盘的上方。
键盘的响应通知有以下几种
- UIKeyboardWillShowNotification : 键盘即将出现时
- UIKeyboardDidShowNotification : 键盘出现后
- UIKeyboardWillHideNotification : 键盘将要隐藏时
- UIKeyboardDidHideNotification : 键盘隐藏后
- UIKeyboardWillChangeFrameNotification : 键盘即将更改
frame
属性时 - UIKeyboardDidChangeFrameNotification : 键盘更改
frame
属性后
每个通知都包含了一个 userInfo 的字典,其中包含了键盘的信息,例如 size
尺寸等,我们可以根据字典里的数据对界面进行调整,以保证输入框的显示。
具体用法:
// 设置一个 UITextField对象
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 30)];;
tf.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:tf];
// 注册通知事件
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoradWillShow:) name:UIKeyboardWillShowNotification object:nil];
复制代码
同时实现调用方法
-(void)keyBoradWillShow:(NSNotification *)sender{NSLog(@"keyBoradWillShow");NSLog(@"%@",sender.userInfo);
}
复制代码
当点击输入框弹出键盘时,我们可以看到控制台打印数据:
keyBoradWillShow
{UIKeyboardAnimationCurveUserInfoKey = 7;UIKeyboardAnimationDurationUserInfoKey = "0.25";UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 271}}";UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 871.5}";UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 600.5}";UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 271}}";UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 465}, {414, 271}}";UIKeyboardIsLocalUserInfoKey = 1;
}
复制代码
关于格式化输入内容
想要自定义输入内容的大小、颜色、字体样式等信息,可以使用 NSAttributedString
来格式化文本内容。
想要格式化驶入内容可以通过委托代理的方式来验证输入的文本信息并且进行修改,其代理方法将在下文中列出。
设置覆盖视图
UITextField
支持在输入框的两侧添加一个自定义的视图。 可以根据其 leftViewMode
/rightViewMode
属性设置视图的出现时间。
leftViewMode
/rightViewMode
是一个 UITextFieldViewMode
类型的属性,其中包含
- UITextFieldViewModeNever : 不显示
- UITextFieldViewModeWhileEditing :编辑中显示
- UITextFieldViewModeUnlessEditing : 非编辑时显示
- UITextFieldViewModeAlways : 一直显示
具体添加方法 :
UIButton* overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:@"bookmark"] forState:UIControlStateNormal];
[overlayButton addTarget:self action:@selector(displayBookmarks:)forControlEvents:UIControlEventTouchUpInside];
overlayButton = CGRectMake(0, 0, 28, 28);// Assign the overlay button to a stored text field
self.textField.leftView = overlayButton;
self.textField.leftViewMode = UITextFieldViewModeAlways;
复制代码
UITextFieldDelegate
UITextFieldDelegate
可以用来监听输入框 Text
值的变化,我们可以通过实现委托协议来对输入的文本内容进行控制。
- 输入框能否开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
- 输入框能否结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
- 输入框开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- 输入框完成编辑,
reason
代表结束编辑原因(似乎只能在TVOS中使用)
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason;
- 输入框完成编辑,不带
reason
参数
- (void)textFieldDidEndEditing:(UITextField *)textField;
- 当按下
clear
按钮时,能否清除输入框中的内容
- (BOOL)textFieldShouldClear:(UITextField *)textField;
- 当按下
return
按钮时,能否进行return
操作
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
- 当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
UITextField相关属性
文字类型设置
属性 | 类型 | 解释 |
---|---|---|
text | NSString | 输入框中的文本内容 |
attributedText | NSAttributedString | 输入框中的富文本文本内容 |
placeholder | NSString | 在未输入内容情况下的文字展示 |
attributedPlaceholder | NSAttributedString | 在未输入内容情况下的富文本展示 |
defaultTextAttributes | NSDictionary<NSAttributedStringKey, id> | 默认富文本格式 |
font | UIFont | 文字格式 |
textColor | UIColor | 文字色彩 |
textAlignment | NSTextAlignment | 文字对齐方式 |
UITextBorderStyle | 枚举类型 | 边框属性 |
typingAttributes | NSDictionary<NSAttributedStringKey, id> | 风格属性 |
文本大小属性
属性 | 类型 | 解释 |
---|---|---|
adjustsFontSizeToFitWidth | BOOL | 是否根据输入框宽度调整字体大小 |
minimumFontSize | CGFloat | 字体的最小缩放比例 |
这两个属性需要同时使用才能够实现文本根据输入框长度的自动缩放
编辑行为相关属性
属性 | 类型 | 解释 |
---|---|---|
editing | BOOL | 只读,是否正在编辑 |
clearsOnBeginEditing | BOOL | 是否在编辑时清空原有文本内容 |
clearsOnInsertion | BOOL | 插入文本是否替换之前的文本 |
allowsEditingTextAttributes | BOOL | 是否允许编辑文本属性 |
文本框的背景属性
属性 | 类型 | 解释 |
---|---|---|
borderStyle | UITextBorderStyle | 输入框的边框类型 |
background | UIImage | 输入框的背景图片 |
disabledBackground | UIImage | 当输入框禁用时的背景图片 |
UITextBorderStyle
是一个枚举类型,其中包括
- UITextBorderStyleNone : 无边框
- UITextBorderStyleLine : 细边框
- UITextBorderStyleBezel : 粗边框
- UITextBorderStyleRoundedRect : 圆角边框
文本框附带图层
属性 | 类型 | 解释 |
---|---|---|
clearButtonMode | UITextFieldViewMode | 清除按钮模式 |
leftView | UIView | 左侧按钮 |
leftViewMode | UITextFieldViewMode | 左侧按钮模式 |
rightView | UIView | 右侧按钮 |
rightViewMode | UITextFieldViewMode | 右侧按钮模式 |
UITextFieldViewMode | 枚举类型 | 输入框的模式 |
UITextFieldViewMode
内包含
- UITextFieldViewModeNever : 无其他视图
- UITextFieldViewModeWhileEditing : 在编辑时显示视图
- UITextFieldViewModeUnlessEditing : 在非编辑时显示视图
- UITextFieldViewModeAlways : 一直显示视图
UITextField方法
- 控制文本位置
- (CGRect)textRectForBounds:(CGRect)bounds;
- 绘制文本的位置,一般在自定义时重写,不直接调用
- (void)drawTextInRect:(CGRect)rect;
- 修改在没有输入文字时显示的文本的位置
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
- 修改边框的位置
- (CGRect)borderRectForBounds:(CGRect)bounds;
- 修改清除按钮的位置
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
- 修改左侧视图的位置
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
- 修改右侧视图的位置
- (CGRect)rightViewRectForBounds:(CGRect)bounds;
OC_UITextField相关推荐
- 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)
博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...
最新文章
- tensorflow 运行报错 runtime error use a closed session
- Boost:bind绑定全局占位符的测试程序
- python条件输出_python基础(二)条件判断、循环、格式化输出
- Can't process attribute android:fillColor=@color/gray,添加vector属性报错解决方法
- volatile与多线程
- [jquery] 删除文章的时候弹出确认窗口
- 盘点一下 在Python中安装包的三种方法
- java爬虫 抓取国家统计局:统计用区划代码和城乡划分代码(抓取省市区镇县办事处村委会数据)生成json
- linux内核配置选项
- 设计模式-行为型软件设计模式(四)
- Redis Gossip广播通信 1.gossip原理概述
- 迅雷总提示版权什么的。。
- 几种常用的操作系统调度策略
- 疫情影响供求再平衡,运用芝商所原油期货对冲风险
- python无向加权图_NetworkX:无向加权图的近似/不精确子图同构
- 预训练模型 Fine-tuning
- abap视图字段限制_创建表维护视图(SE54)
- 用Kettle的一套流程完成对整个数据库迁移
- 岁月,依一抹浅香于心间
- C语言malloc两次,C语言malloc()的一个问题——堆越界的一个错误。