您的位置:首页 > 产品设计 > UI/UE

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

2016-05-19 15:57 309 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: