概述

  • UITextField在界面中显示可编辑文本区域的对象。
  • 您可以使用文本字段来使用屏幕键盘从用户收集基于文本的输入。键盘可以配置许多不同类型的输入,如纯文本,电子邮件,数字等等。文本字段使用目标操作机制和委托对象来报告在编辑过程中所做的更改。
    除了基本的文本编辑行为之外,还可以将叠加视图添加到文本字段以显示其他信息并提供其他可定位控件。您可以为诸如书签按钮或搜索图标等元素添加自定义叠加视图。文本字段提供内置的叠加视图来清除当前文本。自定义覆盖视图的使用是可选的

属性和方法

初始化

 UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(, , , )];

设置占位文本

textField.placeholder = @"请输入文字";

设置文本

textField.text = @"测试";

设置文本的颜色

textField.textColor = [UIColor redColor];

设置文本的字体

textField.font = [UIFont systemFontOfSize:];

设置文本的对齐方式

textField.textAlignment = NSTextAlignmentRight;

设置输入框不能编辑

[textField setEnabled:NO];

设置编辑框中的内容密码显示

textField.secureTextEntry = YES;

启用文本字段时显示的背景图像。该图像显示在文本字段内容的其余部分后面

textField.background = [UIImage imageNamed:@"登录logo"];

设置边框样式(更多边框样式到补充说明中查看)默认样式为UITextBorderStyleNone

textField.borderStyle = UITextBorderStyleRoundedRect;

设置清楚按钮的模式(更多清楚按钮的模式到补充说明中查看)默认样式为UITextFieldViewModeNever

textField.clearButtonMode = UITextFieldViewModeUnlessEditing;

文本字段文本的最小字体大小。当“调整为适合”选项启用时,文本字段会自动更改字体大小以确保文本的最大可读性。你可以使用此属性来指定你认为适合文本的最小字体大小

textField.minimumFontSize = ;

设置键盘类型(更多键盘类型在补充说明中查看)

textField.keyboardType = UIKeyboardTypeNumberPad;

设置键盘上返回键的类型(更多返回类型到补充说明中查看)

textField.returnKeyType = UIReturnKeyJoin;

设置键盘的视觉样式(更多键盘的视觉样式效果到补充说明中查看)

textField.keyboardAppearance = UIKeyboardAppearanceLight;

文本字段的拼写检查行为。此属性决定了拼写检查在打字过程中是启用还是禁用

textField.spellCheckingType = UITextSpellCheckingTypeNo;
此属性决定了拼写检查在打字过程中是启用还是禁用。启用拼写检查后,文本对象会为所有拼写错误的单词生成红色下划线。如果用户点击拼写错误的单词,则文本对象向用户呈现可能的更正列表。
此属性的默认值是default,启用自动更正时启用拼写检查。此属性中的值将覆盖用户在“设置”>“常规”>“键盘”中设置的拼写检查设置。

文本字段的自动纠正行为。此属性确定在输入过程中自动更正是启用还是禁用

textField.autocorrectionType = UITextAutocorrectionTypeYes;

自动大写样式适用于键入的文本。此属性决定在什么时候自动按下shift键

textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

设置左边试图(注意:需要先设置左边视图的显示模式为UITextFieldViewModeAlways)

textField.leftViewMode = UITextFieldViewModeAlways;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
imageView.image = [UIImage imageNamed:@"验证码"];
textField.leftView = imageView;

设置右边视图(注意:需要先设置右变视图的显示模式为UITextFieldViewModeAlways)

textField.rightViewMode = UITextFieldViewModeAlways;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
imageView.image = [UIImage imageNamed:@"验证码"];
textField.rightView = imageView;

代理方法

询问代理是否应该在指定的文本字段中开始编辑

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        

// return NO to disallow editing.

告诉代理在指定的文本字段中开始编辑

- (void)textFieldDidBeginEditing:(UITextField *)textField;           

// became first responder

询问代理是否应在指定的文本字段中停止编辑

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;          

// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end

告诉代理对指定的文本字段停止编辑

- (void)textFieldDidEndEditing:(UITextField *)textField;             

// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called

告诉代理对指定的文本字段停止编辑

- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); 

// if implemented, called in place of textFieldDidEndEditing:

询问代理是否应该更改指定的文本

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   

// return NO to not change text

询问代理是否应删除文本字段的当前内容

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

补充说明

设置UITextField占位文字的颜色的两种办法

第一种

KVC修改,如果不先设置占位文字,占位文字的颜色是不管用的:

textField.placeholder = @"占位字符";
textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

第二种

通过attributedPlaceholder属性修改占位文字颜色

NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入占位文字" attributes:@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:textField.font
}];
textField.attributedPlaceholder = attrString;

自定义键盘(输入视图)

UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(, , self.view.bounds.size.width, )];
redView.backgroundColor = [UIColor redColor];
textField.inputView = redView;
textField.textColor = [UIColor redColor];

【转发】https://www.jianshu.com/p/740cd34f870b

最新文章

  1. iOS - ViewController的生命周期
  2. 采用UDP协议的PIC32MZ ethernet bootloader
  3. 遗传算法的简单应用-巡回旅行商(TSP)问题的求解
  4. ActiveMQ: 搭建Broker集群(cluster)
  5. 图解Android - Android GUI 系统 (1) - 概论
  6. Python基础 (yield生成器)
  7. NIO中Selector分析
  8. linux概念之用户,组及权限
  9. tomcat web.xml 配置
  10. Android之Handler探索
  11. 握手(bestcode#42)
  12. 基于visual Studio2013解决C语言竞赛题之1053洗牌
  13. Linux下安装与配置Nginx
  14. 剖析Prometheus的内部存储机制
  15. PAT乙级--1003
  16. SQLSERVER数据库死锁与优化杂谈
  17. ES5新增内容
  18. Javascript入门(五)数组操作、循环语句
  19. Activiti的25张表
  20. 11月28日 记录一个错误❌,看ruby on rails --active support core extensions--present? && presence && duplicable?

热门文章

  1. [Excel] 一些实用的函数式子
  2. [蓝桥杯][基础训练]FJ的字符串
  3. 题解【洛谷P5436】【XR-2】缘分
  4. Ansible - 配置文件
  5. 优化mysql
  6. iso15693芯片读写工具套件 icode-slix2读写 nfc type 5 tag读写
  7. MAC记住 git的用户名密码
  8. Java将数据进行分组处理
  9. Abaqus 子模型法 和 子结构法
  10. 关于SQL