如何在上侧而不是常规底侧增加textview的高度?

时间:2017-07-13 12:28:20

标签: ios swift

我想根据textview的内容增加textview的高度。我尝试了不同的方式,但我没有得到解决方案来增加向上的高度。 Attached image

2 个答案:

答案 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