UI-UITextField常用属性及键盘收回的几种方式

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)


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图解AI

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值