iOS界面编程-UITextField

本文详细介绍了UITextField的基本概念、常用方法和属性,包括键盘管理、文本编辑行为、背景设置等,并提供了实例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、介绍

        UITextField继承自UICotrol-》UIView-》UIResponder-》NSObject,一个UITextField对象是一个用来显示可编辑文本和当用户按下返回键时,发送动作消息给目标对象。典型使用于获取用户少量文字输入,完成快速动作,例如基于用户输入的文字的搜索。

        另外对于那些基于文字的行为,UITextField支持覆盖视图的显示去显示另外的信息,你可以使用自定义覆盖视图去显示诸如书签按钮或搜索图标。UITextField类也提供内置的按钮用来清除当前文字。

      文本域对象,提供委托对象用来处理与编辑相关的通知。你可以使用这个委托去定制控件的编辑行为以及提供某种动作发生后的向导。具体操作可以查看UITextFieldDelegate委托对象。

二、相关方法和属性

 1、管理键盘

   当用户点击一个文本输入框,文本输入框变成第一响应对象自动请求系统显示键盘。因为系统键盘显示后,肯定会遮盖住一些用户界面,造成当前输入框显示不完整。用户需要自己进行处理,以便在输入数据时,不遮挡输入界面。另外一些系统对象,如table view 帮助滑动第一响应者到合适的地方。但是如果table view滑动到底端之后还是需要用户自己去处理

    用户可以设置当按下键盘中的某个按钮,通常如return键,可以用来隐藏键盘。主要是通过调用resignFirstResponder方法完成操作,这样当调用此方法,则完成当前的编辑会话操作。

   用户可以定制键盘的外表,通过UITextInputTraits protocol 提供的属性去操作。你可以使用这些属性去指定键盘的类型。你可以使用这些属性指定键盘的输入类型。你也可以配置键盘的输入行为,例如输入的单词首字母大写。

  2.键盘通知

 当系统显示或者隐藏键盘时,会发出一些 键盘通知事件。这些通知包含了键盘的一些信息,诸如键盘的大小,可以用来移动视图。注册这些通知是得到这些键盘信息的唯一方式,键盘传递的相关消息如下:

 3状态保存



4、访问文字属性

@property(nonatomic,copy) NSString *text 文本域显示的文字

@property(nonatomic,copy) NSAttributedString *attributedText textfield显示的文本样式

@property(nonatomic,copy) NSString *placeholder 没有输入文字时,显示的占位符。

@property(nonatomic,copy) NSAttributedString *attributedPlaceholder  占位字符的文本样式。

@property(nonatomic,copy) NSDictionary <NSString *,id> *defaultTextAttributes

@property(nonatomic,strong) UIFont *font 文字字体

@property(nonatomic,strong) UIColor *textColor 文字颜色,默认为黑色,不能设置为nil

@property(nonatomic)NSTextAlignment textAlignment 文字对齐方式

@property(nonatomic,copy) NSDictionary <NSString *,id> *typingAttributes  用户正在输入文字的属性


 5、设置文字的尺寸大小

@property(nonatomic)BOOL adjustsFontSizeToFitWidth 是否自动

@property(nonatomic)CGFloat minimumFontSize 允许的最小的字体


6、管理编辑行为

@property(nonatomic,readonly, getter=isEditing)BOOL editing 布尔值,获取是否处于编辑状态。

@property(nonatomic)BOOL clearsOnBeginEditing 用来设置是否开始编辑时,清除以前的状态。

@property(nonatomic)BOOL clearsOnInsertion 

@property(nonatomic)BOOL allowsEditingTextAttributes


7、设置背景外表

@property(nonatomic)UITextBorderStyle borderStyle  边框样式

@property(nonatomic,strong) UIImage *background 背景图片

@property(nonatomic,strong) UIImage *disabledBackground textfied不可用时,显示的背景

@property(nonatomic)UITextFieldViewMode clearButtonMode

@property(nonatomic,strong) UIView *leftView

@property(nonatomic)UITextFieldViewMode leftViewMode

@property(nonatomic,strong) UIView *rightView

@property(nonatomic)UITextFieldViewMode rightViewMode


8、访问委托

@property(nonatomic,weak) id<UITextFieldDelegate > delegate

9 绘图和定位覆盖

一般这些方法不能直接调用,需要重写这些方法

- (CGRect)textRectForBounds:(CGRect)bounds 返回文本的绘制区域

- (void)drawTextInRect:(CGRect)rect

- (CGRect)placeholderRectForBounds:(CGRect)bounds

- (void)drawPlaceholderInRect:(CGRect)rect

- (CGRect)borderRectForBounds:(CGRect)bounds

- (CGRect)editingRectForBounds:(CGRect)bounds

- (CGRect)clearButtonRectForBounds:(CGRect)bounds

10、代替系统的输入视图

@property(readwrite,strong) UIView *inputView

@property(readwrite,strong) UIView *inputAccessoryView

三、实际例子

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 74, self.view.frame.size.width - 50, 31)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = @"please input password";
//  textField.secureTextEntry = YES;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//  textField.keyboardType = UIKeyboardTypeEmailAddress;
    textField.returnKeyType = UIReturnKeyDone;
    textField.font = [UIFont systemFontOfSize:14.0f];
    textField.textColor = [UIColor cyanColor];
    textField.delegate = self;
    textField.contentVerticalAlignment = UIViewContentModeCenter;
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 44, 44);
    [button setTitle:@"test" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    
    [textField setRightView:button];
    [textField setRightViewMode:UITextFieldViewModeAlways];
    [self.view addSubview:textField];



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

house.zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值