UITextView动态改变高度

  func textViewDidChange(_ textView: UITextView) {
        
        let textViewWidth = textView.frame.size.width
        let content = textView.text
        let contentSize = content?.size(font: UIFont.pingFangSCRegularFont(ofSize: 16))
        if let width = contentSize?.width, textViewWidth > 0 {
            var numLine = Int(ceil(width / textViewWidth))
            if numLine > currentLineNum {
                textView.snp.updateConstraints { (make) in
                    make.height.equalTo(24 + numLine * 24)
                }
            } else if (numLine < currentLineNum) {
                if numLine <= 1 {
                    textView.snp.updateConstraints { (make) in
                        make.height.equalTo(48)
                    }
                } else {
                    textView.snp.updateConstraints { (make) in
                        make.height.equalTo(24 + numLine * 24)
                    }
                }
            }
            if numLine == 0 {
                numLine = 1
            }
            currentLineNum = numLine
        }
    }

你可能感兴趣的:(UITextView动态改变高度)