UITextField的初始化,设置位置以及大小。
UITextField *textfield=[[UITextFieldalloc] initWithFrame:CGRectMake(20,250,130,30)];
设置UITextField的边框。textfield.borderStyle=UITextBorderStyleLine;
UITextField有四种边框类型,分别如下:
typedef NS_ENUM(NSInteger, UITextBorderStyle) {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
};
UITextFile的占位符,placeholder,可以提示用户输入什么。
textfield.placeholder=@"请输入用户名。";
textfield.backgroundColor=[UIColorredColor];//设置背景颜色
textfield.font=[UIFontfontWithName:@"Arial"size:20];//设置字体类型以及大小
textfield.textColor=[UIColorblueColor];//设置文字的颜色
textfield.background=[UIImageimageNamed:@"checkbox_checked.png"];//设置背景,说明,根据用户使用的边框样式,有时显示背景颜色,有时显示背景图片
textfield.disabledBackground=[UIImageimageNamed:@"checkbox_unchecked.png"];//设置不可用是的背景,
textfield.clearButtonMode=UITextFieldViewModeUnlessEditing;//输入框中是否有叉号,用于一次性删除文本框内容。有四种情况。
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever,//从不出现
UITextFieldViewModeWhileEditing,//编辑的时候出现
UITextFieldViewModeUnlessEditing,//不编辑的时候出现
UITextFieldViewModeAlways//总是出现
};
txtfield.secureTextEntry=YES;//设置文本框是否以密码形式显示,有YES与NO。
txtfield.autocorrectionType=UITextAutocorrectionTypeYes;//自动更正,有三中情况
typedef NS_ENUM(NSInteger, UITextAutocorrectionType) {
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo,
UITextAutocorrectionTypeYes,
};
txtfield.clearsOnBeginEditing=YES;//再次编辑时,清空文本框的内容
txtfield.autocapitalizationType=UITextAutocapitalizationTypeWords;设置字母大小写。有四种情况。
typedefNS_ENUM(NSInteger, UITextAutocapitalizationType) {
UITextAutocapitalizationTypeNone,//不自动大写
UITextAutocapitalizationTypeWords,//单词首字母大写
UITextAutocapitalizationTypeSentences,//句子首字母大写
UITextAutocapitalizationTypeAllCharacters,//所有字符大写
};
txtfield.textAlignment=NSTextAlignmentRight;//水平对齐方式
txtfield.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;//垂直对齐方式
txtfield.adjustsFontSizeToFitWidth=YES;//是否调整文字大小根据文本框的宽度。
txtfield.minimumFontSize=10;//设置允许的最小字体大小
txtfield.keyboardType=UIKeyboardTypeDefault;//设置键盘的类型,主要有一下:
typedef NS_ENUM(NSInteger, UIKeyboardType) {
UIKeyboardTypeDefault, // 默认键盘类型,支持所有
UIKeyboardTypeASCIICapable, // 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, // 电话键盘,或者
UIKeyboardTypeURL, // URL键盘,支持.com按钮
UIKeyboardTypeNumberPad, // 数字键盘
UIKeyboardTypePhonePad, // 点击键盘
UIKeyboardTypeNamePhonePad, // 支持电话输入与人名
UIKeyboardTypeEmailAddress, // 电子邮件键盘
#if __IPHONE_4_1 <= __IPHONE_OS_VERSION_MAX_ALLOWED
UIKeyboardTypeDecimalPad, // 数字键盘,支持数字以及小数点
#endif
#if __IPHONE_5_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
UIKeyboardTypeTwitter, // 优化的键盘,方便输入 @ #等。
#endif
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,// Deprecated//过时了
};
txtfield.returnKeyType=UIReturnKeySearch;//设置return将变成什么键,有以下几种:
typedefNS_ENUM(NSInteger, UIReturnKeyType) {
UIReturnKeyDefault,//显示return,
UIReturnKeyGo,//显示Go
UIReturnKeyGoogle,//显示搜索,系统是英文的话,显示Search
UIReturnKeyJoin,//显示Join
UIReturnKeyNext,//显示下一项或者Next
UIReturnKeyRoute,//显示Route
UIReturnKeySearch,//显示Search
UIReturnKeySend,//显示Send
UIReturnKeyYahoo,//显示Search
UIReturnKeyDone,//显示Don
UIReturnKeyEmergencyCall,//显示EmergencyCall
};
说明,对于上面的UIReturnKeyGoogle,UIReturnKeyYahoo,网上有人说分别显示Google,Yahoo,我在模拟器上面显示为Search,在真机上面也是显示“搜索”,我的真机版本
iOS 5.1的。有测试显示google与yahoo的可以说一下,同时也欢迎大家指正与补充。
txtfield.keyboardAppearance=UIKeyboardAppearanceDefault; //设置键盘的外观,如两种样式
typedefNS_ENUM(NSInteger, UIKeyboardAppearance) {
UIKeyboardAppearanceDefault, //默认外观,浅灰色
UIKeyboardAppearanceAlert // 深灰石墨色.
};
//UITextField的左侧或者右侧添加图片,显示显示右侧添加图片txtfield.rightView=[[[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"checkbox1_checked.png"]]autorelease];
txtfield.rightViewMode=UITextFieldViewModeAlways;
ViewMode的情况同上面的一样,也有四种,
typedefNS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
};
除了UITextField对象的风格选择,我们还可以定制化UITextField对象,对他的不同方法进行重写,UITextField派生自UIControl,所有UIControl类中的通知系统在UITextField中也可以使用。