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

UITextFiled输入框的使用(详细)

2016-01-04 15:59 405 查看
UITextFiled输入框的使用

1.基本使用
<1>UITextField的基本使用	[重点]
<2>键盘回收(别忘了设置代理)	[重点]

2.属性的设置
<1>字体
<2>文本颜色
<3>设置密码输入键盘
<4>设置键盘类型
<5>设置清除模式
<6>设置空白提示
<7>设置是否自动大写
<8>是否自动显示修正后的单词
<9>设置文本对齐方式
<10>设置背景图片
<11>左侧提示图片

3.项目中常用(扩展知识, 现在不是特别会也没什么关系)

<1>键盘事件的处理(使用通知)
<2>点击背景则回收键盘(背景图片添加手势)
<3>输入长度的控制
<4>自定义键盘(emoji表情)
<5>键盘上的附件区域
<6>email地址验证(正则表达式)

==============知识点详解==============

<1>UITextField的基本使用

UITextField *nameTextField = [[UITextField alloc] init];
//设置位置
nameTextField.frame = CGRectMake(100, 100, 200, 30);
//细节: 输入框有边框类型的, 如果直接创建, 不会显示边框
// UITextBorderStyleNone没有边框. 需要设置背景图片
nameTextField.borderStyle = UITextBorderStyleRoundedRect;
//显示出来
[self.view addSubview:nameTextField];

<2>键盘回收(别忘了设置代理)

//细节: 系统不会自动回收键盘, 需要我们编程实现

//具体应该如何实现?
//  输入框有很多事件(开始编辑, 结束编辑, 文本改变,返回)
//需求: 我们需要去处理这些事件, 但是问题何在?
//存在的问题:  我们预先不知道用户什么时候进行这些操作
//          但是textFiled自己知道用户什么时候进行操作

//解决的方式:  当textFiled事件发生了之后, 通知我们进行处理
//使用代理
//<1> 设置代理
//  协议中规定了代理对象发送的消息
//<2>让self所在的类遵守delegate指定的协议
//<3>需要实现协议中必须实现的方法
nameTextField.delegate = self;

//这个方法是键盘上的Return被点击之后执行
//  这个方法是由 textFiled调用的
//  textFiled保存了当前的对象指针   self.delegate
//  当事件发生的时候, 会通过self.delegate调用现在这个方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
//输入框上点击了之后, 获得了焦点, 输入框变成第一响应者
//          自动弹出虚拟键盘

//如何回收键盘
//意思: 放弃作为第一响应者
[textField resignFirstResponder];

return YES;
}

<3>字体
nameTextField.font = [UIFont systemFontOfSize:24];

<4>文本颜色
nameTextField.textColor = [UIColor redColor];

<5>设置密码输入键盘
//添加一个密码输入框
UITextField *passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 140, 200, 30)];
passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
passwordTextField.delegate = self;
[self.view addSubview:passwordTextField];

passwordTextField.secureTextEntry = YES;

<6>设置键盘类型
//需求: 输入框输入的只是电话号的话
nameTextField.keyboardType = UIKeyboardTypePhonePad;

<7>设置清除模式
//细节: 这个按键只有在输入框中有文本的时候才会出现
nameTextField.clearButtonMode = UITextFieldViewModeAlways;
passwordTextField.clearButtonMode = UITextFieldViewModeAlways;

<8>设置空白提示
//细节: 空白提示会在输入文本后自动消失
nameTextField.placeholder = @"用户名";
passwordTextField.placeholder = @"密码";

<9>设置是否自动大写
//需求: 去掉自动大小写
nameTextField.autocapitalizationType =
UITextAutocapitalizationTypeNone;

<10>是否自动显示修正后的单词
//需求: 关闭拼写自动提示
nameTextField.autocorrectionType = UITextAutocorrectionTypeNo;

<11>设置文本对齐方式
nameTextField.textAlignment = NSTextAlignmentCenter;

项目中常用(扩展知识, 现在不是特别会也没什么关系)

<1>设置背景图片

//细节: 边框类型需要设置UITextBorderStyleNone
nameTextField.background = [UIImage imageNamed:@"inputImage.png"];

<2>左侧提示图片

UIImageView *headView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
headView.image = [UIImage imageNamed:@"logo64X64.png"];

nameTextField.leftView = headView;
[headView release];
//细节: 需要左侧视图的显示模式
nameTextField.leftViewMode = UITextFieldViewModeAlways;

<3>键盘事件的处理(使用通知)——解决遮挡的问题
//需求: 虚拟键盘弹出后遮挡住的控件显示出来

//使用ios系统事件通知来解决
//  什么是 事件通知???
//  生活: 邮件到了之后, 门房通知我们
//  (1)需要告诉系统, 我们要用哪个方法去处理这个事件
//  (2)事件的处理方法中完成, 控件遮挡的处理

//获取一个通知中心的单例对象
//参数name: 表示事件的名称
//   UIKeyboardWillShowNotification键盘即将显示
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealKeyboadShow:) name:UIKeyboardWillShowNotification object:nil];

//键盘隐藏的事件通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealKeyboadHide:) name:UIKeyboardWillHideNotification object:nil];

//键盘显示事件通知的处理方法
-(void)dealKeyboadShow:(NSNotification *)noti
{
NSLog(@"dealKeyboadShow");

[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:0.5];

//处理键盘的遮挡
loginButton.frame = CGRectMake(100, 210, 80, 30);
registerButton.frame = CGRectMake(190, 210, 80, 30);

[UIView commitAnimations];
}

-(void)dealKeyboadHide:(NSNotification *)noti
{
NSLog(@"dealKeyboadShow");

[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:0.5];

//处理键盘的遮挡
loginButton.frame = CGRectMake(100, 300, 80, 30);
registerButton.frame = CGRectMake(190, 300, 80, 30);

[UIView commitAnimations];
}

<4>点击背景则回收键盘(背景图片添加手势)

//重点: 处理背景图片的点击事件
//需要做的事情: 为图片添加点击事件处理?
//新的知识点: 手势
//创建了轻击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backImageClick:)];
//为背景图添加轻击手势
backView.userInteractionEnabled = YES;
[backView addGestureRecognizer:tap];
[tap release];

-(void)backImageClick:(UITapGestureRecognizer *)tap
{
NSLog(@"backImageClick");

[nameTextField resignFirstResponder];
[passwordTextField resignFirstResponder];

}

<5>自动切换到下一个输入框

//实现方法回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];

//如果是第一个输入框输完了, 切换到下一个中进行输入
//表示: 如果两个指针相等, 则为同一对象
if(textField == nameTextField)
{
//密码输入框变成第一响应者
[passwordTextField becomeFirstResponder];
}

return YES;
}

<6>输入长度的控制

//是在文本输入框中每次插入文本的时候执行
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//需求: 限制输入的长度为8个字符以下
//有问题版本
/*
if (nameTextField.text.length<8) {
return YES;
}
*/

if(textField == nameTextField)
{
NSMutableString *mstr = [NSMutableString stringWithString:nameTextField.text];
//获取插入之后的字符串
[mstr insertString:string atIndex:range.location];

return mstr.length <= 8;
}
return YES;
}

<7>自定义键盘(emoji表情)
//如何自定义键盘???
//		创建一个自定义键盘(一个view上摆了很多button)

messageTextField.inputView = [self createEmotionView];

//获取创建的表情键盘
-(UIView *)createEmotionView
{
UIView *myEmotionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 160)];
myEmotionView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];

//CTRL+CMD+SPACE

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