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相关属性

文字类型设置

属性类型解释
textNSString输入框中的文本内容
attributedTextNSAttributedString输入框中的富文本文本内容
placeholderNSString在未输入内容情况下的文字展示
attributedPlaceholderNSAttributedString在未输入内容情况下的富文本展示
defaultTextAttributesNSDictionary<NSAttributedStringKey, id>默认富文本格式
fontUIFont文字格式
textColorUIColor文字色彩
textAlignmentNSTextAlignment文字对齐方式
UITextBorderStyle枚举类型边框属性
typingAttributesNSDictionary<NSAttributedStringKey, id>风格属性

文本大小属性

属性类型解释
adjustsFontSizeToFitWidthBOOL是否根据输入框宽度调整字体大小
minimumFontSizeCGFloat字体的最小缩放比例

这两个属性需要同时使用才能够实现文本根据输入框长度的自动缩放

编辑行为相关属性

属性类型解释
editingBOOL只读,是否正在编辑
clearsOnBeginEditingBOOL是否在编辑时清空原有文本内容
clearsOnInsertionBOOL插入文本是否替换之前的文本
allowsEditingTextAttributesBOOL是否允许编辑文本属性

文本框的背景属性

属性类型解释
borderStyleUITextBorderStyle输入框的边框类型
backgroundUIImage输入框的背景图片
disabledBackgroundUIImage当输入框禁用时的背景图片

UITextBorderStyle是一个枚举类型,其中包括

  • UITextBorderStyleNone : 无边框
  • UITextBorderStyleLine : 细边框
  • UITextBorderStyleBezel : 粗边框
  • UITextBorderStyleRoundedRect : 圆角边框

文本框附带图层

属性类型解释
clearButtonModeUITextFieldViewMode清除按钮模式
leftViewUIView左侧按钮
leftViewModeUITextFieldViewMode左侧按钮模式
rightViewUIView右侧按钮
rightViewModeUITextFieldViewMode右侧按钮模式
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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值