UITextField 是iOS开发中用的非常多的一种控件,主要是供用户输入单行信息的。下面来详细介绍UITextField。

1、常用属性

<span style="font-size:14px;">// 设置和获取文本内容,默认nil
@property(nonatomic,copy) NSString *text;
// 设置文本内容颜色
@property(nonatomic,retain) UIColor *textColor;
// 设置字体
@property(nonatomic,retain) UIFont *font
// 对齐样式
@property(nonatomic) NSTextAlignment textAlignment;
// 设置风格,默认没有风格,需要设置
@property(nonatomic) UITextBorderStyle borderStyle;
// 提示用户输入内容文本
@property(nonatomic,copy) NSString *placeholder;
// 用户编辑时是否clear内容,默认为NO
@property(nonatomic) BOOL clearsOnBeginEditing;
// 自适应调整字体大小,默认为NO
@property(nonatomic) BOOL adjustsFontSizeToFitWidth;
// 设置代理
@property(nonatomic,assign) id<UITextFieldDelegate> delegate;
// 设置背景,需要将textField实例的风格设置为None
@property(nonatomic,retain) UIImage *background;
// 设置textField不可用时的背景图片
@property(nonatomic,retain) UIImage *disabledBackground;
// 设置是否可编辑
@property(nonatomic,readonly,getter=isEditing) BOOL editing;
// 清除按钮的模式,默认不出现
@property(nonatomic) UITextFieldViewMode clearButtonMode;
// 自定义左视图
@property(nonatomic,retain) UIView *leftView;
// 自定义左视图出现的模式
@property(nonatomic) UITextFieldViewMode leftViewMode;
// 不用系统键盘,自定义键盘
@property (readwrite, retain) UIView *inputView;
// 系统键盘和自定义键盘共存
@property (readwrite, retain) UIView *inputAccessoryView;
// 自动大写类型
@property(nonatomic) UITextAutocapitalizationType autocapitalizationType;
// 检查拼写是否正确
@property(nonatomic) UITextAutocorrectionType autocorrectionType;
// 修改键盘类型
@property(nonatomic) UIKeyboardType keyboardType;
// 修改返回类型
@property(nonatomic) UIReturnKeyType returnKeyType;
// 是否安全输入,比如用户输入密码
@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry;</span>

iOS中选中输入框会自动弹出键盘,但是如果需要关闭键盘则需要自己手动控制,一般关闭键盘,要么在点击returnKeyType的时候用代理方法关闭,或者点击真个View的空白区域来关闭。

关闭键盘的方法,点参照我的另一片文章-----IOS隐藏键盘的几种方式

2、常用的代理方法

<span style="font-size:14px;">// 将要开始输入时调用
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(@"开始输入");
return YES;
}
// 将要输入结束时调用
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(@"输入结束");
return YES;
}
// 清除文字按钮点击事件
- (BOOL)textFieldShouldClear:(UITextField *)textField {
NSLog(@"清除输入内容了");
return YES;
}
// 键盘上的return按钮
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//隐藏输入键盘
[textField resignFirstResponder];
return YES;
}</span>

创建UITextField,如果用代码创建的话,实例代码如下:

<span style="font-size:14px;">UITextField *textfield = [[UITextField alloc]
initWithFrame:CGRectMake(20, 240, 200, 30)];
// 禁止首字母大写
textfield.autocapitalizationType = UITextAutocapitalizationTypeNone;
// 设置键盘类型
textfield.keyboardType = UIKeyboardTypeNamePhonePad;
// 输入框的边框类型
textfield.borderStyle = UITextBorderStyleRoundedRect;
// 设置委托代理
textfield.delegate = self;
// 键盘上的return按钮类型
textfield.returnKeyType = UIReturnKeyDone;
// 是否安全输入,是的话,输入内容将为星号
textfield.secureTextEntry = NO;
// 清除按钮模式
textfield.clearButtonMode = UITextFieldViewModeAlways;
// 输入框中的文本颜色
textfield.textColor = [UIColor redColor];
// 输入框的字体
textfield.font = [UIFont boldSystemFontOfSize:14];</span>

UITextField 好像用法比较简单,唯一需要注意的就是键盘的隐藏的,其他的特殊功能,可能还没用到吧,以后遇到相关问题再来补充记录。

最新文章

  1. vertx核心类之VertxImpl
  2. msbuild ConfuserEx.Build 加密
  3. 4412开发板Android教程——Android平台简介
  4. 破解受保护的excel中的密码
  5. MAXIMO移动解决方案在库存管理中的PDA应用系统
  6. 配置android模拟器
  7. ruby condition
  8. 自定义 Lint 规则简介
  9. UVA 10194 Football (aka Soccer)
  10. 百度编辑器ueditor如何配置
  11. 字符串转化为json的三种方法
  12. MD中bitmap源代码分析--SYNC IO和RAID5的补充
  13. hdoj 3555 Bomb(DFA+dp)
  14. Mars之android的Handler(2)
  15. ssh配置文件及问题解决
  16. 一起刷LeetCode
  17. C++ 常见面试题目
  18. php值callback类型和匿名函数(闭包)
  19. 如何查看sonarqube的版本
  20. 2019-03-27-day020-单继承与多继承

热门文章

  1. 计科1702冯亚杰C语言程序设计预备作业
  2. git报错:&#39;fatal:remote origin already exists&#39;怎么处理?附上git常用操作以及说明。
  3. Intellij IDEA自动编译问题
  4. ubuntu安装fat32和exfat文件系统支持
  5. 聊聊LightProbe原理实现以及对LightProbe数据的修改
  6. 【已解决】IIS搭建 asp.net core 项目后 其他电脑访问不到资源文件
  7. 【事务】&lt;查询不到同一调用方法其它事务提交的更新&gt;解决方案
  8. Unity发布至IOS的流程(踩坑记录)
  9. Docker学习笔记4: Docker-Compose—简化复杂容器应用的利器
  10. android M Launcher之LauncherModel (二)