美文网首页
动态改变textview的高度

动态改变textview的高度

作者: guoguojianshu | 来源:发表于2020-01-10 10:14 被阅读0次
    - (float) QiuTanTiYuHeightForTextView: (UITextView *)textView QiuTanTiYuWithText: (NSString *) strText{
        CGSize constraint = CGSizeMake(textView.contentSize.width , CGFLOAT_MAX);
        CGRect size = [strText boundingRectWithSize:constraint
                                            options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                         attributes:@{NSFontAttributeName: TYZXFANGSCMEDIUM(14)}
                                            context:nil];
        float textHeight = size.size.height + 22.0;
        return textHeight;
    }
    
    -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
        CGRect QiuTanTiYuFrame = textView.frame;
        float QiuTanTiYuHeight = [self QiuTanTiYuHeightForTextView:textView QiuTanTiYuWithText:textView.text];
        QiuTanTiYuFrame.size.height = QiuTanTiYuHeight;
        [UIView animateWithDuration:0.5 animations:^{
            textView.frame = QiuTanTiYuFrame;
        }];
        [self.QiuTanTiYuTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(QiuTanTiYuHeight);
        }];
    
        return YES;
    }
    
    -(void)textViewDidChange:(UITextView *)textView{
        if (textView.text.length>0) {
            self.QiuTanTiYuPlaceHolderLabel.hidden = YES;
        }else{
            self.QiuTanTiYuPlaceHolderLabel.hidden = NO;
        }
    }
    

    相关文章

      网友评论

          本文标题:动态改变textview的高度

          本文链接:https://www.haomeiwen.com/subject/nnrpactx.html