ios 自定义键盘输入框 输入框跟随文字换行变宽 输入框随着键盘消失出现位置进行变化

1.首先通过xib创建一个自定义输入框view

2.在自定义的输入框View中添加block,并实现textView的代理方法

[objc] view plain copy
  1. @property (nonatomic,copy) void(^changeInputViewFrame)(float viewH);  
  2.   
  3.   
  4. - (void)textViewDidChange:(UITextView *)textView{  
  5.   
  6.     if (textView != self.inputTextView) {  
  7.         return;  
  8.     }  
  9.       
  10.     CGSize size = [self.inputTextView.text sizeWithAttributes:@{UIFontDescriptorCascadeListAttribute:[UIFont systemFontOfSize:15.0]}];  
  11.     CGFloat singleH = size.height;  
  12.     NSInteger lines = (NSInteger)self.inputTextView.contentSize.height/singleH;  
  13.     NSLog(@"行数行数======%ld",lines);  
  14.     if (lines>self.minLines) {  
  15.         self.minLines = lines;  
  16.         if (self.changeInputViewFrame&&lines<=7) {  
  17.             self.changeInputViewFrame(singleH);  
  18.         }  
  19.     }  
  20.     if (lines<self.minLines) {  
  21.         self.minLines = lines;  
  22.         if (self.changeInputViewFrame&&lines>2) {  
  23.             self.changeInputViewFrame(singleH*(-1));  
  24.         }  
  25.     }  
  26.   
  27.   
  28. }  

3.在使用该自定义view的类中实现以下方法

 

 

[objc] view plain copy
  1. __weak CustomChatViewController * weakSelf = self;  
  2.     self.inputBar.changeInputViewFrame = ^(float viewH){  
  3.         weakSelf.inputBar.frame = CGRectMake(0, weakSelf.inputBar.frame.origin.y-viewH, weakSelf.view.frame.size.width, weakSelf.inputBar.frame.size.height+viewH);  
  4.         //自定义输入view刷新约束  
  5.         [weakSelf.inputBar layoutIfNeeded];  
  6.     };  

以上三步即可实现自定义输入框高度随着输入行数的变化而变化

 

4.在添加自定义输入框的类中实现输入框随着键盘出现消失,位置跟随变化

 

[objc] view plain copy
  1. /增加监听,当键盘出现或改变时收出消息  
  2.     [[NSNotificationCenter defaultCenter] addObserver:self  
  3.                                              selector:@selector(keyboardWillShow:)  
  4.                                                  name:UIKeyboardWillShowNotification  
  5.                                                object:nil];  
  6.       
  7.     //增加监听,当键退出时收出消息  
  8.     [[NSNotificationCenter defaultCenter] addObserver:self  
  9.                                              selector:@selector(keyboardWillHide:)  
  10.                                                  name:UIKeyboardWillHideNotification  
  11.                                                object:nil];  

[objc] view plain copy
  1. - (void)keyboardWillShow:(NSNotification *)aNotification  
  2. {  
  3.     //获取键盘的高度  
  4.     /* 
  5.      iphone 6: 
  6.      中文 
  7.      2014-12-31 11:16:23.643 Demo[686:41289] 键盘高度是  258 
  8.      2014-12-31 11:16:23.644 Demo[686:41289] 键盘宽度是  375 
  9.      英文 
  10.      2014-12-31 11:55:21.417 Demo[1102:58972] 键盘高度是  216 
  11.      2014-12-31 11:55:21.417 Demo[1102:58972] 键盘宽度是  375 
  12.       
  13.      iphone  6 plus: 
  14.      英文: 
  15.      2014-12-31 11:31:14.669 Demo[928:50593] 键盘高度是  226 
  16.      2014-12-31 11:31:14.669 Demo[928:50593] 键盘宽度是  414 
  17.      中文: 
  18.      2015-01-07 09:22:49.438 Demo[622:14908] 键盘高度是  271 
  19.      2015-01-07 09:22:49.439 Demo[622:14908] 键盘宽度是  414 
  20.       
  21.      iphone 5 : 
  22.      2014-12-31 11:19:36.452 Demo[755:43233] 键盘高度是  216 
  23.      2014-12-31 11:19:36.452 Demo[755:43233] 键盘宽度是  320 
  24.       
  25.      ipad Air: 
  26.      2014-12-31 11:28:32.178 Demo[851:48085] 键盘高度是  264 
  27.      2014-12-31 11:28:32.178 Demo[851:48085] 键盘宽度是  768 
  28.       
  29.      ipad2 : 
  30.      2014-12-31 11:33:57.258 Demo[1014:53043] 键盘高度是  264 
  31.      2014-12-31 11:33:57.258 Demo[1014:53043] 键盘宽度是  768 
  32.      */  
  33.     NSDictionary *userInfo = [aNotification userInfo];  
  34.     NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];  
  35.     CGRect keyboardRect = [aValue CGRectValue];  
  36.     float height = keyboardRect.size.height;  
  37.     self.inputBar.frame = CGRectMake(0, self.view.frame.size.height-50-height, self.view.frame.size.width, 50);  
  38. }  
  39.   
  40. //当键退出时调用  
  41. - (void)keyboardWillHide:(NSNotification *)aNotification{  
  42.   
  43.     [UIView animateWithDuration:0.5 animations:^{  
  44.         self.inputBar.frame = CGRectMake(0, self.view.frame.size.height-50, self.view.frame.size.width, 50);  
  45.     }];  
  46. }  

键盘失去响应根据自己需求添加
posted @ 2018-06-26 23:40  sundaysios  阅读(390)  评论(0)    收藏  举报