用法用例:textView.keyboardtype =UIKeyboardTypeNumberPad;二、键盘外观
UIKeyboardAppearanceDefault, // 默认外观:浅灰色 UIKeyboardAppearanceAlert, //深灰/石墨色 } UIKeyboardAppearance;
UIReturnKeyGoogle, //标有Google的蓝色按钮,用于搜索 UIReturnKeyJoin, //标有Join的蓝色按钮 UIReturnKeyNext, //标有Next的蓝色按钮 UIReturnKeyRoute, //标有Route的蓝色按钮 UIReturnKeySearch, //标有Search的蓝色按钮 UIReturnKeySend, //标有Send的蓝色按钮 UIReturnKeyYahoo, //标有Yahoo!的蓝色按钮,用于搜索 UIReturnKeyDone, //标有Done的蓝色按钮 UIReturnKeyEmergencyCall, //紧急呼叫按钮} UIReturnKeyType;
用法用例:textView.returnKeyType=UIReturnKeyGo;四、自动大写
UITextAutocapitalizationTypeNone, //不自动大写 UITextAutocapitalizationTypeWords, //单词首字母大写 UITextAutocapitalizationTypeSentences, //句子首字母大写 UITextAutocapitalizationTypeAllCharacters, //所有字母大写 } UITextAutocapitalizationType;
开启安全输入主要是用于密码或一些私人数据的输入,此时会禁用自动更正和自此缓存。以上内容都可以在 inspector中设置:
(1)在 ViewController.h 中声明一个方法:
- (IBAction)textFiledReturnEditing:(id)sender;(2)在 ViewController.m 中实现这个方法:
-(IBAction)textFiledReturnEditing:(id)sender { [sender resignFirstResponder];}让这三个文本框都映射到 textFiledReturnEditing 方法,不过此时的事件应当是 Did End On Exit ,具体操作是:打开 Assistant Editor ,左边打开 ViewController.xib ,右边打开 ViewController.h ,在 Xcode 最右边打开 Connector Inspector ,然后在 View 中选择第一个文本框,在 Connector Inspector 中找到 Did End On Exit ,从它右边的圆圈中拉出映射线,映射到 ViewController.h 的 textFiledReturnEditing 方法,如下图:
给其他两个文本框进行同样的操作。现在,已经实现了轻触 Return 键关闭键盘。
2、下面介绍轻触背景关闭键盘。
(1)在 ViewController.h 文件中添加方法声明代码:
- (IBAction)backgroundTap:(id)sender;(2)在ViewController.m中实现这个方法:
- (IBAction)backgroundTap:(id)sender { [firstField resignFirstResponder]; [secondField resignFirstResponder]; [thirdField resignFirstResponder];}(3)让 View 映射到这个方法,不过事先,我们先要改变 View 的类型。打开xib,选中 View ,打开 Identity Inspector ,在 class 中选择 UIControl :4)打开Assistant Editor ,左边打开 ViewController.xib ,右边打开 ViewController.h ,在Xcode最右边打开 Connector Inspector ,在 ViewController.xib 中选择 Control ,在 Connector Inspector 中找到 Touch Down ,从它右边的圆圈中拉出映射线,映射到 ViewController.h 的 backgroundTap 方法,如下图:运行结果:
打开键盘之后,在背景区域点击一下,键盘就会向下收起来。
三.解决虚拟键盘挡住UITextField的方法
因为屏幕太小的缘故,一个键盘跳出来总是把输入框挡住,所以需要移动屏幕来匹配键盘
#pragma mark -#pragma mark 解决虚拟键盘挡住UITextField的方法- (void)keyboardWillShow:(NSNotification *)noti //键盘输入的界面调整 //键盘的高度 float height = 216.0; CGRect frame = self.view.frame; frame.size = CGSizeMake(frame.size.width, frame.size.height - height); [UIView beginAnimations:@"Curl"context:nil];//动画开始 [UIView setAnimationDuration:0.30]; [UIView setAnimationDelegate:self]; [self.view setFrame:frame]; [UIView commitAnimations];-(BOOL)textFieldShouldReturn:(UITextField *)textField // When the user presses return, take focus away from the text field so that the keyboard is dismissed. NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height); //CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height); self.view.frame = rect; [UIView commitAnimations]; [textField resignFirstResponder]; return YES; - (void)textFieldDidBeginEditing:(UITextField *)textField CGRect frame = textField.frame; int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216 NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyBoard" context:nil]; [UIView setAnimationDuration:animationDuration]; float width = self.view.frame.size.width; float height = self.view.frame.size.height; if(offset 0) CGRect rect = CGRectMake(0.0f, -offset,width,height); self.view.frame = rect; [UIView commitAnimations]; #pragma mark -
只要在代码中加入这三个文件,然后将自身delegate
控制器添加UITextFieldDelegate
@interface ViewController : UIViewController UITextFieldDelegate在viewDidLoad中添加:
- (void)viewDidLoad [super viewDidLoad]; self.firstTextField.delegate=self; self.secondTextField.delegate=self; self.thirdTextField.delegate=self;}
但是这里经常会有屏幕移动后不能返回的问题,这里的解决方案就是
- (IBAction)backgroundTap:(id)sender { [self.firstTextField resignFirstResponder]; [self.secondTextField resignFirstResponder]; [self.thirdTextField resignFirstResponder]; NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height); self.view.frame = rect;}