UITextField常用属性及键盘收回的几种方式
2016-05-19 15:57
309 查看
UITextField 常用属性
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)];
通过触摸事件来实现键盘的回收
- (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;
}
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;
}
相关文章推荐
- C#和sqlserver中生成新的32位GUID
- UIKit控件直接显示网页文字内容
- UINavigationController 基本用法
- 24.UIViewController
- 4.SUID、SGID、SBIT、chattr权限
- UIImagePickerController本地图片视频,相机录像机使用
- ThreadPoolExecutor运转机制及BlockingQueue详解
- android requestCode和resultCode
- 例题 8-3 UVA - 1152 4 Values whose Sum is 0(和为0的4个值)(二分枚举)
- 优化UITableViewCell高度计算的那些事
- Fiddler(七) | QuickExec
- 23.UIView
- jqyery easyui $.messager.alert 位置代码
- UGUI-界面多分辨率适配
- web动画的方式 aa3e requestAnimationFrame
- maven build脚本笔记
- 关于iOS中的弹出窗口----UIAlertController
- UUID
- 解决ADT大量出现"Unexpected value from nativeGetEnabledTags: 0"的问提
- iOS学习之——UIButton