iOS textViewCell高度根据textView内容动态改变,并实现键盘吸附功能

先看最终效果图:

 

 

本来想在网上找的,结果发现还没人提供demo,一般都做一种效果,cell高度自适应或者键盘吸附,像我要的这种效果没找到,没办法,我只能自己撸了.

 

其实实现并不难,主要注意以下几点

 

1. cell中的textView要用约束设置(我用的Masonary) 高度的约束要撑满

1 [textView mas_makeConstraints:^(MASConstraintMaker *make) {
2         make.left.equalTo(self.titleLbl.mas_right).offset(10);
3         make.top.equalTo(self).offset(10);
4         make.bottom.equalTo(self).offset(-10);
5         make.right.equalTo(self).mas_offset((-20));
6     }];

 

 

2. 设置tableView的rowHeight属性为 UITableViewAutomaticDimension 并且给 estimatedRowHeight 赋值 给一个估算的高度

 

 // 设置可变的cell高度,系统自动计算
  _tableView.rowHeight = UITableViewAutomaticDimension;
  _tableView.estimatedRowHeight = 45;

 

 

 

3. 在自定义cell中实现textView文字改变的代理  并且添加键盘的弹出,收起的通知

 

 

 1 -(void)addNotificationKeyBoard{
 2     
 3     [[NSNotificationCenter defaultCenter]addObserver:self
 4                                             selector:@selector(keyBoardShow:)
 5                                                 name:UIKeyboardWillShowNotification object:nil];
 6     
 7     [[NSNotificationCenter defaultCenter]addObserver:self
 8                                             selector:@selector(keyBoardHidden:)
 9                                                 name:UIKeyboardWillHideNotification object:nil];
10     
11 }
12 
13 - (void)dealloc {
14     
15     [[NSNotificationCenter defaultCenter] removeObserver:self];
16 }

 

 1 #pragma mark  - textView delegate
 2 - (void)textViewDidChange:(UITextView *)textView {
 3     // 通过代理 保存textView的内容,确保不被复用
 4     if ([self.delegate respondsToSelector:@selector(textView:didChangeText:)]) {
 5         [self.delegate textView:self didChangeText:textView.text];
 6     }
 7     CGRect bounds  = textView.bounds;
 8     CGRect oldBounds = textView.bounds;
 9     // 计算 text view 的高度
10     CGSize maxSize = CGSizeMake(bounds.size.width, CGFLOAT_MAX);
11     CGSize newSize = [textView sizeThatFits:maxSize];
12     // 让 table view 重新计算高度
13     UITableView *tableView = [self tableView];
14     bounds.size = newSize;
15     textView.bounds = bounds;
16     // 当textView的高度改变的时候刷新cell高度
17     if (oldBounds.size.height != newSize.height) {
18         [tableView beginUpdates];
19         [tableView endUpdates];
20     }
21     //cell 高度增加时候 改变contenOffset
22     if (oldBounds.size.height < newSize.height ) {
23         [tableView layoutIfNeeded];
24         CGFloat height = newSize.height - oldBounds.size.height;
25         [UIView animateWithDuration:.2 animations:^{
26             tableView.contentOffset = CGPointMake(0, tableView.contentOffset.y + height);
27         }];
28     }
29 }
30 
31 #pragma mark  - keyBoard Notification
32 
33 - (void)keyBoardHidden:(NSNotification *)notifi{
34     // 恢复tableView的Y值
35     [UIView animateWithDuration:.2 animations:^{
36          [self tableView].top = 0;
37     }];
38 }
39 
40 -(void)keyBoardShow:(NSNotification *)notifi{
41     // 过滤textView 拿到当前响应的textView
42     if (![self.textView isFirstResponder]) return;
43     // 获取键盘高度
44     float height = [[notifi.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
45  // 获取textView的位置 转换为keyWidow的坐标
46     CGRect rect = [self.textView convertRect:self.textView.bounds toView:[UIApplication sharedApplication].keyWindow];
47     CGFloat locationY = CGRectGetMaxY(rect);
48     
49     // 键盘遮住cell的时候改变tableView的Y值
50     if (locationY > (SHEIGHT - height)) {
51         [UIView animateWithDuration:.2 animations:^{
52             [self tableView].top -= (height + locationY- SHEIGHT);
53         }];
54     }
55 }

 

 

 

最后想看demo的小伙伴 请进传送门 https://github.com/YH-Coding/TextViewCellDemo

如果有不懂的,有更好的实现方案或是发现bug欢迎留言讨论

 

如果对你有帮助的话,欢迎Star哦~~   Thanks♪(・ω・)ノ

 

end

posted @ 2018-06-11 14:51  YH_Coding  阅读(380)  评论(0编辑  收藏  举报