您的位置:首页 > 产品设计 > UI/UE

IOS --控件UITextField详解

2013-10-11 20:38 405 查看
UITextField,IOS中的输入控件,最常用到的控件之一。

创建UITextField并添加到视图上

[cpp] view plaincopy

// 创建UITextField

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

// 设置UITextField的样式

textField.borderStyle = UITextBorderStyleRoundedRect;

只有设置了borderStyle才能被正确的显示,默认的样式一共有4种,分别是

typedef enum {

UITextBorderStyleNone,

UITextBorderStyleLine,

UITextBorderStyleBezel,

UITextBorderStyleRoundedRect

} UITextBorderStyle;

文字字体相关的属性设置

[cpp] view plaincopy

// 设置默认文字内容

textField.text = @"text";

// 文字对齐方式

textField.textAlignment = NSTextAlignmentLeft;

// 文字颜色

textField.textColor = [UIColor redColor];

// 提示信息内容

textField.placeholder = @"请输入内容";

// 字体

textField.font = [UIFont fontWithName:@"Arial" size:12.0];

文字对齐方式如下,IOS6已前的UITextAlignmentLeft已经过时

enum {

NSTextAlignmentLeft = 0,

NSTextAlignmentCenter = 1,

NSTextAlignmentRight = 2,

NSTextAlignmentJustified = 3,

NSTextAlignmentNatural = 4,

};

[cpp] view plaincopy

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

textField.clearButtonMode = UITextFieldViewModeAlways;

typedef enum {

UITextFieldViewModeNever, 重不出现

UITextFieldViewModeWhileEditing, 编辑时出现

UITextFieldViewModeUnlessEditing, 除了编辑外都出现

UITextFieldViewModeAlways  一直出现

} UITextFieldViewMode;

[cpp] view plaincopy

//设置输入框的背景颜色,此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉

textField.backgroundColor = [UIColor whiteColor];

//设置背景

textField.background = [UIImage imageNamed:@"dd.png"];

//设置背景

textField.disabledBackground = [UIImage imageNamed:@"cc.png"];

[cpp] view plaincopy

//是否纠错

textField.autocorrectionType = UITextAutocorrectionTypeNo;

typedef enum {

UITextAutocorrectionTypeDefault, 默认

UITextAutocorrectionTypeNo,  不自动纠错

UITextAutocorrectionTypeYes, 自动纠错

} UITextAutocorrectionType;

[cpp] view plaincopy

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

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

//再次编辑就清空

textField.clearsOnBeginEditing = YES;

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

textField.adjustsFontSizeToFitWidth = YES;

//设置自动缩小显示的最小字体大小

textField.minimumFontSize = 20;

[cpp] view plaincopy

//首字母是否大写

textField.autocapitalizationType = UITextAutocapitalizationTypeNone;

typedef enum {

UITextAutocapitalizationTypeNone, 不自动大写

UITextAutocapitalizationTypeWords, 单词首字母大写

UITextAutocapitalizationTypeSentences, 句子的首字母大写

UITextAutocapitalizationTypeAllCharacters, 所有字母都大写

} UITextAutocapitalizationType;

[cpp] view plaincopy

//设置键盘的样式

textField.keyboardType = UIKeyboardTypeNumberPad;

typedef enum {

UIKeyboardTypeDefault,  默认键盘,支持所有字符

UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘

UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符

UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符

UIKeyboardTypeNumberPad,  数字键盘

UIKeyboardTypePhonePad,   电话键盘

UIKeyboardTypeNamePhonePad,  电话键盘,也支持输入人名

UIKeyboardTypeEmailAddress,  用于输入电子 邮件地址的键盘

UIKeyboardTypeDecimalPad,  数字键盘 有数字和小数点

UIKeyboardTypeTwitter,  优化的键盘,方便输入@、#字符

UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,

} UIKeyboardType;

[cpp] view plaincopy

//return键的样式

textField.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;

[cpp] view plaincopy

//键盘外观

textField.keyboardAppearance=UIKeyboardAppearanceDefault;

typedef enum {

UIKeyboardAppearanceDefault, 默认外观,浅灰色

UIKeyboardAppearanceAlert,   深灰 石墨色

} UIReturnKeyType;

[cpp] view plaincopy

//文本垂直对齐

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

UItextField UITextView 有关键盘的设置


autocapitalizationType 设置键盘自动大小写的属性
UITextAutocapitalizationTypeNone


autocorrectionType
property 设置是否有自动修改提示 UITextAutocorrectionTypeNo


enablesReturnKeyAutomatically Boolean值-设置在用户没有输入是returnKey禁用,默认值NO



keyboardAppearance 设置键盘显示方式 除了默认模式 还有一个UIKeyboardAppearanceAlert模式

keyboardType 设置键盘类型 UIKeyboardTypePhonePad 等

returnKeyType 设置renturnKey按键上的提示文字 UIReturnKeyGo UIReturnKeyNext

secureTextEntry BOOL值 -- 设置是否是密码保护模式输入

本文系整理后转载,出处:http://blog.csdn.net/qq5306546/article/details/8843049 及http://blog.163.com/cai_1111/blog/static/198815027201110101112176/,感谢原作者分享
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: