UITextField 常用属性
UITextField 是UIControl的子类,UIControl又是UIView的子类,所以也是一个视图,只不过比UIView多了两个功能,1.文字显示,2.文本编辑
使用过程分四步:
1.创建对象
2.配置属性
3添加到父视图
4.释放所有权
1.创建对象
UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 180, 50)]; text.backgroundColor = [UIColor yellowColor]; [_view addSubview:text]; [text release];
2.设置text的边框样式(圆角)
text.borderStyle = UITextBorderStyleRoundedRect;
3.设置text默认显示文字(但是不作为文本内容的一部分)
text.placeholder = @"请输入用户名";
4.设置text文字
text.text = @"什么破烂”;
5.设置文本颜色
text.textColor = [UIColor blackColor];
6.设置文本的对齐方式
text.textAlignment = NSTextAlignmentCenter;
7.设置文字字体
text.font = [UIFont systemFontOfSize:18];
8.设置输入框是否可编辑
text.enabled = YES;
9.设置当开始编辑时,是否清除框中内容
text.clearsOnBeginEditing = YES;
10.设置密码格式(输入框中内容是否以点的形式显示)
text.secureTextEntry = YES;
11.设置弹出键盘的样式(数字键盘)
text.keyboardType = UIKeyboardTypeNumberPad;
12.键盘右下角显示样式
text.returnKeyType = UIReturnKeyGo;
13.设置tag值
text.tag = 120;
14.键盘回收的几种方式(还有很多方法)
-
(void)viewDidLoad {
[super viewDidLoad];
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(20, 50, 300, 100)];
tf.tag = 100;
tf.backgroundColor = [UIColor redColor];
[self.view addSubview:tf];
//方法1 通过代理来完成键盘回收
tf.delegate =self;
//方法2 通过手势轻按,实现键盘回收
UITapGestureRecognizer *tag =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tag:)];
[self.view addGestureRecognizer:tag];
[tf performSelector:@selector(returnKey)];//方法3 点击键盘return触发事件发生,键盘消失
[tf addTarget:self action:@selector(returnKey) forControlEvents:UIControlEventEditingDidEndOnExit];
}
//方法4 通过触摸事件来实现键盘的回收
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITextField *tf = (UITextField *)[self.view viewWithTag:100];
[tf resignFirstResponder];
}
//方法3 点击键盘return触发事件发生,键盘消失
-(void)returnKey{
UITextField *tf = (UITextField *)[self.view viewWithTag:100];
[tf endEditing:YES];
}
//方法2 通过手势轻按,实现键盘回收
-(void)tag:(UITapGestureRecognizer *)tag{
UITextField *tf = (UITextField *)[self.view viewWithTag:100];
[tf endEditing:YES];
}
//方法1 通过代理来完成键盘回收 - (BOOL)textFieldShouldReturn:(UITextField *)textField;{
[textField resignFirstResponder];
return YES;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201112134447670.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0JhaUh1YVhpdTEyMw==,size_16,color_FFFFFF,t_70#pic_center)