IOS控件之UITextField用法及注意点

主要总结工作中常用到的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  //键盘隐藏之后发送通知

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值