01、创建TextField
//创建TextField
UITextField *myTextField = [[UITextField alloc] init];
//设置大小
myTextField.frame = CGRectMake(70, 150, 200, 30);
//背景颜色
myTextField.backgroundColor = [UIColor magentaColor];
//添加到当前视图显示
[self.view addSubview:myTextField];
02、center
//将myTextField置于当前视图的中心
myTextField.center = self.view.center;
03、 text
//输入文本
myTextField.text = @"世界, 你好!";
04、placeholder
//占位符
myTextField.placeholder = @"请输入信息";
05、textColor
//设置字体颜色
myTextField.textColor = [UIColor redColor];
06、textAlignment 文本对齐方式
//文本文字横向居中
myTextField.textAlignment = NSTextAlignmentCenter;
07、enablesReturnKeyAutomatically
//默认为No,如果设置为Yes,文本框中没有输入任何字符的话,右下角的返回按钮是disabled的。如果文本框中已经输入字符,则不禁用。
myTextField.enablesReturnKeyAutomatically = YES;
08、borderStyle
设置边框样式,只有设置了才会显示边框样式
//圆角
myTextField.borderStyle = UITextBorderStyleRoundedRect;
typedef enum {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
} UITextBorderStyle;
09、background 背景图片
//background 背景图片
myTextField.background = [UIImage imageNamed:@"navbar_bg_normal-64"];
注意:当UITextField的属性borderStyle设置为UITextBorderStyleRoundedRect的时候,background设置是无效的,即:
//圆角形式
myTextField.borderStyle = UITextBorderStyleRoundedRect;
//background 背景图片
myTextField.background = [UIImage imageNamed:@"navbar_bg_normal-64"];
10、font 字体
//设置输入框内容字体的大小
myTextField.font = [UIFont systemFontOfSize:17];
//设置输入框内容字体的样式和大小
myTextField.font = [UIFont fontWithName:@"Arial" size:20];
11、clearButtonMode 清除文本按钮模式:输入框会有一个×号,用于一次性清除文本内容
//编辑的时候出现
myTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
属性有如下:
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever, //重不出现
UITextFieldViewModeWhileEditing, //编辑时出现
UITextFieldViewModeUnlessEditing,//除了编辑的时候都出现
UITextFieldViewModeAlways //总是出现
};
12、secureTextEntry
//安全文本模式,一般用于输入密码时需要,每输入一个字符就会变成“.”。
myTextField.secureTextEntry = YES; //默认是NO
13、autocorrectionType键盘自动纠正类型
属性有:
typedef NS_ENUM(NSInteger, UITextAutocorrectionType) {
UITextAutocorrectionTypeDefault, //默认
UITextAutocorrectionTypeNo, //不自动纠错
UITextAutocorrectionTypeYes, //自动纠错
};
//会自动纠正
myTextField.autocorrectionType = UITextAutocorrectionTypeYES;
DEMO如图:
![这里写图片描述](https://img-blog.csdn.net/20160702190941020)
//不会自动纠正
myTextField.autocorrectionType = UITextAutocorrectionTypeNo;
DEMO如图:
![这里写图片描述](https://img-blog.csdn.net/20160702191107725)