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:UIControlEventEditingDid
//方法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; 
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值