摘要:
textView的创建;设置属性;代理方法;隐藏键盘;键盘弹出时调整textView的位置,像手机短信的效果
一、新建一个textView
02 | UITextView *textView = [[[UITextView alloc] init] autorelease]; |
05 | textView.delegate = self; |
08 | textView.font = [UIFont systemFontOfSize:16]; |
11 | textView.contentInset = UIEdgeInsetsMake(-11, -6, 0, 0); |
14 | textView.scrollEnabled = NO; |
17 | [textView becomeFirstResponder]; |
1 | [self.view addSubview:textView]; |
二、键盘操作
2 | textView.returnKeyType = UIReturnKeyDefault; |
5 | textView.keyboardType = UIKeyboardTypeDefault; |
三、隐藏键盘的几种方式
个人还是认为最方便的是在键盘上加上一个ToolBar,在上面加上一个按钮来隐藏键盘
①在键盘上加上隐藏按钮
02 | UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)]; |
05 | [topView setBarStyle:UIBarStyleBlack]; |
08 | UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil]; |
10 | UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil]; |
13 | UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@ "完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard)]; |
16 | NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil]; |
17 | [topView setItems:buttonsArray]; |
19 | [textView setInputAccessoryView:topView]; |
2 | - ( void )resignKeyboard { |
3 | [textView resignFirstResponder]; |
最终效果
还有几种也可隐藏键盘的方式
②用回车键,前提是你的textView中不需要用到回车键
1 | -( BOOL )textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { |
2 | if ([text isEqualToString:@ "\n" ]) |
4 | [textView resignFirstResponder]; return NO; |
③触摸空白处隐藏键盘
1 | -( void )touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event |
4 | [textView resignFirstResponder]; |
四、使键盘不挡住输入框
在view中添加一个子view,设置此子view的tag值为1000,在此view上添加一个textView和一个发送按钮,如下图;我们要达到textView的键盘弹出时,整个View往上平移,键盘消失,view往下平移的效果,模拟发送短信的界面。
设置textView圆角
2 | [self.textView.layer setCornerRadius:10]; |
①、在viewWillAppear中添加键盘监听事件
4 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; |
7 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil]; |
②、完成①selector中键盘弹出keyboardDidShow:和消失keyboardDidHidden方法
在.m文件#import后面添加
2 | #define kAnimationDuration 0.2 |
键盘出现
02 | -( void )keyboardDidShow:(NSNotification *)notification |
06 | NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; |
10 | [keyboardObject getValue:&keyboardRect]; |
15 | [UIView beginAnimations:nil context:nil]; |
18 | [UIView setAnimationDuration:kAnimationDuration]; |
21 | [(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-kViewHeight, 320, kViewHeight)]; |
23 | [UIView commitAnimations]; |
键盘消失
02 | -( void )keyboardDidHidden |
05 | [UIView beginAnimations:nil context:nil]; |
06 | [UIView setAnimationDuration:kAnimationDuration]; |
08 | [(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-kViewHeight, 320, kViewHeight)]; |
09 | [UIView commitAnimations]; |