- 创建
UITextField *field = [[UITextField alloc]init];
2. 基本属性
field.frame = CGRectMake(10,100, self.view.frame.size.width-20, 40);
field.backgroundColor = [UIColor redColor];
//设置背景图片,前提feild样式是圆角矩形时不可用
field.background = [UIImage imageNamed:@""];
//设置一个当field不是第一编辑状态时显示的背景图片
field.disabledBackground = [UIImage imageNamed:@""];
//设置边框样式
/*
UITextBorderStyleNone,:无
UITextBorderStyleLine,:线性边框
UITextBorderStyleBezel,:直角立体矩形
UITextBorderStyleRoundedRect 圆角矩形
*/
field.borderStyle = UITextBorderStyleRoundedRect;
3. 文字对齐方式
//文字左中右对齐方式
field.textAlignment = NSTextAlignmentLeft;
//文字上中下对齐方式
field.contentVerticalAlignment = 0;
//文字字体
field.font = [UIFont systemFontOfSize:13];
4. 占位符
//占位符
field.placeholder = @"用户名";
5. 是否密文显示及右边❎按钮显示时机
//是否显示密文显示
field.secureTextEntry = NO;
//右边显示删除按钮
/*
UITextFieldViewModeNever, 从不出现
UITextFieldViewModeWhileEditing, 编辑时出现
UITextFieldViewModeUnlessEditing, 除了编辑外多出现
UITextFieldViewModeAlways。一直出现
*/
field.clearButtonMode = UITextFieldViewModeAlways;
//再次编辑清空
field.clearsOnBeginEditing = YES;
6 .左右视图添加
//设置左右视图
UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yonghu.png"]];
imageview.frame = CGRectMake(10,0,20,20);
field.leftView = imageview;
//设置左右视图显示模式
field.leftViewMode = UITextFieldViewModeAlways;
7. 调整左右视图位置
调整左右视图位置我们重写父类的一个方法就好
创建一个继承UITextField的类
重写该方法
-(CGRect)leftViewRectForBounds:(CGRect)bounds{
CGRect rect = [super leftViewRectForBounds:bounds];
rect.origin.x = 10;
return rect;
}
8. 键盘样式和return键改变
/键盘样式///
/*
UIKeyboardTypeDefault, 默认支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘 +-x*
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad, 数字键盘
UIKeyboardTypePhonePad, 电话键盘
UIKeyboardTypeNamePhonePad, 电话键盘,也可以输入人民
UIKeyboardTypeEmailAddress, 电子邮件
UIKeyboardTypeDecimalPad 数字键盘,有数字和小数点
UIKeyboardTypeTwitter。
UIKeyboardTypeWebSearch
UIKeyboardTypeASCIICapableNumberPad
*/
field.keyboardType = UIKeyboardTypeDefault;
//首字母是否大写
/*
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母大写
*/
field.autocapitalizationType = UITextAutocapitalizationTypeNone;
//return键变成什么键
/*
UIReturnKeyDefault, 默认,灰色按钮
UIReturnKeyGo, 标有go蓝色按钮
UIReturnKeyGoogle, google蓝色按钮 搜索
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
*/
field.returnKeyType = UIReturnKeyNext;
//键盘外观
/*
UIKeyboardAppearanceAlert灰色,石墨灰
*/
field.keyboardAppearance = UIKeyboardAppearanceDefault;
field.delegate = self;
[self.view addSubview:field];
9. 自定义键盘用到的属性
//inputview自定义时,系统键盘不在显示,
// UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,200)];
// view.backgroundColor = [UIColor redColor];
// field.inputView = view;
10. 键盘上方添加按钮
//给键盘上方添加按钮
UIView *v= [[UIView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width, 40)];
v.backgroundColor = [UIColor lightGrayColor];
v.userInteractionEnabled = YES;
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-60,0,50, 40)];
[btn setTitle:@"按钮" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnclack) forControlEvents:UIControlEventTouchUpInside];
[v addSubview:btn];
field.inputAccessoryView = v;
11. 实时获取文本框内容变化
//实时获取textfield输入文字内容
[field addTarget:self action:@selector(textFieldDidChang:) forControlEvents:(UIControlEventEditingChanged)];
-(void)textFieldDidChang:(UITextField *)textfield{
NSLog(@"%@",textfield.text);
}
12. 点击其他区域键盘收回
//电击空白处键盘收起
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
[self.view addGestureRecognizer:tap];
13.代理方法
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"将要编辑了调用");
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"已经开始编辑了");
}
//
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
NSLog(@"将要结束编辑");
return YES;
}
//该方法根据键盘是否收起才能调用
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"已经结束编辑");
}
//可以在这里拿到文本框输入完成的文字
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"电击retuRn触发%@",textField);
[textField resignFirstResponder];
return YES;
}
14 响应者属性
//成为第一响应者 只读属性
[textField becomeFirstResponder];
//取消第一响应者,
[textField resignFirstResponder];