UITextView随输入修改高度

这篇文章是为了解决在tableView上加textview,但是cell高度是随着输入实时变化的,以及textview换行是上下抖动问题
话不多说,直接上代码

if (@available(iOS 11.0, *)) {
        _mytable.estimatedRowHeight = 0;
        _mytable.estimatedSectionFooterHeight = 0;
        _mytable.estimatedSectionHeaderHeight = 0;
    }

[_mytable beginUpdates];[_mytable endUpdates];这两个方法可以只刷新cell高度, reloaddata会时输入失去焦点,不赞成使用

- (void)textViewDidChange:(UITextView *)textView {
    _dataArray[textView.tag-100] = textView.text;
    [textView sizeToFit];//使用sizeToFit方法处理textview随输入大小变化以及自动换行问题
    _height=textView.frame.size.height; //_height记录textview高度
    textView.frame = CGRectMake(0, 0, 300, textView.frame.size.height);
    
    [_mytable beginUpdates];
    [_mytable endUpdates];
}

将textview加在cell上,

UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 300, 100) textContainer:nil];
    textView.backgroundColor = [UIColor greenColor];
    textView.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
    textView.textColor = [UIColor colorWithRed:74/255 green:74/255 blue:74/255 alpha:1];
    textView.tag=indexPath.row+100;
     textView.text=_dataArray[indexPath.row];
    [cell.contentView addSubview:textView];
    textView.delegate = self;
    // 以下两行代码是为了换行不抖动
    textView.layoutManager.allowsNonContiguousLayout=NO;
    textView.scrollEnabled = NO;
    [textView sizeToFit];//使用sizeToFit方法处理textview随输入大小变化以及自动换行问题
    textView.frame = CGRectMake(0, 0, 300, textView.frame.size.height);
    _height=textView.frame.size.height; //_height记录textview高度

我这个是只有一个cell有输入框,所以简单处理了,如果有多行的,也可以加以区分处理。是不是很简单啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值