textView 自适应高度

参考

改变textView Frame的值
@implementation TextViewCell
- (void)textViewDidChange:(UITextView *)textView
{
  CGRect bounds = textView.bounds;
  // 计算 text view 的高度
  CGSize maxSize = CGSizeMake(bounds.size.width, CGFLOAT_MAX);
  CGSize newSize = [textView sizeThatFits:maxSize];
  bounds.size = newSize;
  textView.bounds = bounds;
  // 让 table view 重新计算高度
  UITableView *tableView = [self tableView];
  [tableView beginUpdates];
  [tableView endUpdates];
}
text Marsonry布局
   [_iContentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_top);
        make.left.mas_equalTo(self.contentView.mas_left).offset(kAspectWidth(10));
        make.right.mas_equalTo(self.contentView.mas_right).offset(kAspectWidth(-10));
        make.height.mas_greaterThanOrEqualTo(kAspectHeight(50));
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(kAspectHeight(-10));
    }];

注意点
1.使用 iOS 8 的特性自动计算 cell 高度,或者在 heightForRow 中自己实现计算高度的代码。
2.UITextView 的 scrollEnable 要设置 NO
3.更新 table view 的高度使用 beginUpdates 和 endUpdates
4.Text view 更新内容后要保存数据,以免重新加载 cell 时数据丢失

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容