我想根据textview的内容增加textview的高度。我尝试了不同的方式,但我没有得到解决方案来增加向上的高度。 Attached image
答案 0 :(得分:2)
在textViewDidChange委托方法中更改UITextView的框架,
func textViewDidChange(_ textView: UITextView) {
let fixedWidth = textView.frame.size.width
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
var textViewFrame = textView.frame
textViewFrame = CGRect.init(x: textViewFrame.origin.x, y: textViewFrame.origin.y+textViewFrame.height-newSize.height, width: max(newSize.width, fixedWidth), height: newSize.height)
textView.frame = textViewFrame
}
它将从底部增加UITextView的大小。
答案 1 :(得分:0)
设置文本后,在viewWillLayoutSubView()
中let contentSize = textView.sizeThatFits(textView.bounds.size)
之后,您将能够使用contentSize高度:
height: contentSize.height