[英]Dynamically change text view height based on number of lines
我正在根據文本視圖中的行數動態更改文本視圖的高度。 我希望文本視圖在第六行之后開始增加高度。 我當前的實現方式導致文本視圖的高度不斷增加,因為行數始終大於以前的行數。 我還希望在刪除行時縮小文本視圖:
func textViewDidChange(_ textView: UITextView) {
let lineCount = numberOfLines(textView: textView)
if lineCount > previousLineCount {
previousLineCount = lineCount - 1
}
print("previous number of lines: \(previousLineCount) current number of lines: \(lineCount)" )
if lineCount > 6 && lineCount > previousLineCount {
self.layoutConstraint.constant += 4
}
}
有任何想法嗎?
鑒於您添加了Leading,Trailing,top和link高度限制,您可以執行
func textViewDidChange(_ textView: UITextView) {
self.heightConstraint.constant = textView.contentSize.height
self.view.layoutIfNeeded()
}
為什么不:
textView.sizeToFit()
並且不要忘記在故事板中禁用滾動或:
textView.scrollEnabled = false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.