UITextView动态改变高度
2020-05-28 本文已影响0人
link_hui
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
}
}