博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS控件之UITextField用法及注意点
阅读量:4046 次
发布时间:2019-05-24

本文共 4018 字,大约阅读时间需要 13 分钟。

主要总结工作中常用到的UI控件的属性,同时使用应注意的地方以及工作中常犯错误,进行了总结.若有不属性的,可自己自己做Demo看一下效果

//初始化方法

UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(x, x, x, x)]

//设置边框样式

tf.borderStyle = UITextBorderStyleRoundedRect;
 typedef enum {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
} UITextBorderStyle;
 
//设置背景颜色,此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉 
  ft.backgroundColor = [UIColor whiteColor];

//设置输入框内容的字体样式和大小

设置 font 属性
//设置字体颜色
ft.textColor = [UIColor redColor];

//输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容

ft.clearButtonMode = UITextFieldViewModeAlways;
typedef enum {
UITextFieldViewModeNever, 重不出现
UITextFieldViewModeWhileEditing, 编辑时出现
UITextFieldViewModeUnlessEditing, 除了编辑外都出现
UITextFieldViewModeAlways  一直出现
} UITextFieldViewMode;

//密文输入

ft.secureTextEntry = YES;

//是否纠错

ft.autocorrectionType = UITextAutocorrectionTypeNo;
typedef enum {
UITextAutocorrectionTypeDefault, 默认
UITextAutocorrectionTypeNo,  不自动纠错
UITextAutocorrectionTypeYes, 自动纠错
} UITextAutocorrectionType;

//再次编辑就清空

ft.clearsOnBeginEditing = YES;

//内容对齐方式

ft.textAlignment = UITextAlignmentLeft;

//内容的垂直对齐方式 UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment

ft.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动 

tf.adjustsFontSizeToFitWidth = YES;
//设置自动缩小显示的最小字体大小
ft.minimumFontSize = 20;

//文本框右侧 小的清除按钮

typedef enum {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
} UITextFieldViewMode;

// 占位文字

tf.placeholder = @”xxxxxx”;

// 默认显示文字

tf.text = @”xxx”;

// 背景

通过给 background 属性赋值
** 若设置背景图片是自定义边框类型的图片,要讲textField 的边框演示设置为 UITextBorderStyleNone,否则自定义边框有可能不起作用

//右侧增加

UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@”xxxxx”]];
ft.rightView=image;
ft.rightViewMode = UITextFieldViewModeAlways;
// 左侧增加图片 则设置 leftView 属性
**这里要注意,默认leftView 和 rightView 是不显示的,要同时设置leftViewMode和rightViewMode.
**若在一个控制器中设置不同文本的 leftView 和 rightView ,一定要实例化不同的 对象,不能讲一个 对象给不同的 文本框使用. 否则,会出现多个 文本框争夺同一个资源,导致程序异常.

// 对齐方式

tf.textAlignment = typedef NS_ENUM(NSInteger, NSTextAlignment) {
NSTextAlignmentLeft,
NSTextAlignmentCenter,
NSTextAlignmentRight
}

// 自定义输入键盘样式

修改属性 inputView

// 自定义键盘演示工具条

修改属性inputAccessoryView

//首字母是否大写

ft.autocapitalizationType = UITextAutocapitalizationTypeNone;
typedef enum {
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
} UITextAutocapitalizationType;

//return键变成什么键

ft.returnKeyType =UIReturnKeyDone;
typedef enum {
UIReturnKeyDefault, 默认 灰色按钮,标有Return
UIReturnKeyGo,  标有Go的蓝色按钮
UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin,标有Join的蓝色按钮
UIReturnKeyNext,标有Next的蓝色按钮
UIReturnKeyRoute,标有Route的蓝色按钮
UIReturnKeySearch,标有Search的蓝色按钮
UIReturnKeySend,标有Send的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮
} UIReturnKeyType;

//键盘类型

tf.keyboardType = typedef NS_ENUM(NSInteger, UIKeyboardType) {
UIKeyboardTypeDefault, 默认演示
UIKeyboardTypeASCIICapable, 英文键盘
UIKeyboardTypeNumbersAndPunctuation, 全键数字键盘
UIKeyboardTypeURL,网址输入键盘
UIKeyboardTypeNumberPad, 没有小数点的数字键盘
UIKeyboardTypePhonePad,带符号的数字键盘
UIKeyboardTypeNamePhonePad,没有符号的英文键盘
UIKeyboardTypeEmailAddress,邮箱键盘
UIKeyboardTypeDecimalPad, 数字键盘
UIKeyboardTypeTwitter,推特键盘
UIKeyboardTypeWebSearch,网页搜索键盘
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, 英文键盘
};

//键盘外观

tf.keyboardAppearance=UIKeyboardAppearanceDefault;
typedef enum {
UIKeyboardAppearanceDefault, 默认外观,浅灰色
UIKeyboardAppearanceAlert,   深灰 石墨色
} UIReturnKeyType;

通知常用常用的监听属性:

UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification

当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。

与之对应的当文本状态发生变化时,常用的键盘监听属性:
UIKeyboardWillShowNotification  //键盘将要出现时发送通知
UIKeyboardDidShowNotification  //键盘弹出后发送通知
UIKeyboardWillHideNotification  //键盘将要隐藏时发送通知
UIKeyboardDidHideNotification  //键盘隐藏之后发送通知

转载地址:http://bfwci.baihongyu.com/

你可能感兴趣的文章
[leetcode BY python]1两数之和
查看>>
微信小程序开发全线记录
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
机器学习实战之决策树(一)
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
Android/Linux 内存监视
查看>>
用find命令查找最近修改过的文件
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
android raw读取超过1M文件的方法
查看>>