iOS -- 不可编辑的UITextView 自动调整高度

     _textView  = [[UITextView alloc] initWithFrame:CGRectMake(13, 10, UIScreenWidth-26, 20)];
        [self.contentView addSubview:_textView];
        _textView.textColor = UIColorFromRGB(0x111111);
        _textView.font = [UIFont systemFontOfSize:16];
        _textView.editable = NO;
        _textView.backgroundColor = [UIColor yellowColor];
        //_textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//相对于父视图不会改变属性
        _textView.textContainer.lineFragmentPadding = 0;
        _textView.textContainerInset = UIEdgeInsetsZero;
      [self textViewDidChange:_textView];


//不可编辑的UITextView 自动调整高度
- (void)textViewDidChange:(UITextView *)textView{
    CGRect frame = textView.frame;
    NSDictionary *attri = @{NSFontAttributeName:textView.font};
    CGSize size = [textView.text boundingRectWithSize:CGSizeMake(280, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attri context:nil].size;
    
    frame.size.height = size.height > 1 ? size.height : 64;
    textView.frame = frame;
    
    //文本居中
    CGRect textFrame = [[textView layoutManager] usedRectForTextContainer:[textView textContainer]];
    CGFloat offsetY = (textView.frame.size.height - textFrame.size.height)/2;
    textView.textContainerInset = UIEdgeInsetsMake(offsetY, 0, 0, 0);
}
原文地址:https://www.cnblogs.com/qiyiyifan/p/8017147.html