自动调整大小的UITextView直到特定高度

7

我有一个应用程序,其中具有聊天功能,使用UITextview输入消息。 UITextview的高度必须是动态的(如果用户输入消息,则根据文本长度更改高度,直到特定高度为止)。

我该如何实现这一点?


https://dev59.com/5FkT5IYBdhLWcg3wXuTP#38714829 或 https://www.youtube.com/watch?v=0Jb29c22xu8 可以帮助您。 - Jay Patel
你可以使用 contentsize 属性。 - Vishal Sharma
1
这是你正在寻找的答案链接,请查看:) https://dev59.com/U3VD5IYBdhLWcg3wNY1Z - junaid
感谢您的评论。还有一个疑问。在5或6行TextView之后,必须执行其原始功能,即(在5或6行后保持恒定高度并进行滚动)。 - Midhun Narayan
5个回答

14
禁用textView的滚动。

enter image description here

enter image description here

增加高度到特定值,然后启用滚动。

提供最大高度约束,然后将此代码添加到您的viewController中。

 class YourViewController: UIViewController, UITextViewDelegate
    {
        @IBOutlet weak var yourTextView: UITextView!

        let textViewMaxHeight: CGFloat = 100
        override func viewDidLoad()
        {
            super.viewDidLoad()
            yourTextView.delegate = self
        }

        func textViewDidChange(textView: UITextView)
        {
            if textView.contentSize.height >= self.textViewMaxHeight
            {
                textView.scrollEnabled = true
            }
            else
                {
                textView.frame.size.height = textView.contentSize.height
                textView.scrollEnabled = false
            }
        }
    }

它并没有回答楼主的问题。禁用只会导致可视文本区域高度修复。 - Abhishek Jain
1
我怎样才能在4行后停止(TextView在4行后必须执行其原始功能)? - Midhun Narayan
@midhunnarayan 你是说在4行之内应该增加高度,之后才启用滚动吗? - Anuraj
@Anuraj 是的,完全正确。 - Midhun Narayan
1
@Anuraj 尝试过了,但在我的情况下,在这个条件之后(textView.contentSize.height >= self.textViewMaxHeight),textview的高度变为0,视图没有显示。 - Midhun Narayan
@MidhunNarayan 你有给TextView设置最大高度约束吗? - Anuraj

7

为您的textView添加高度约束,并创建一个outlet以便您可以调整它。然后,您可以使用UITexfield委托方法textViewDidChange(_ textView: UITextView)来调整高度。

func textViewDidChange(_ textView: UITextView) {

    // get the current height of your text from the content size
    var height = textView.contentSize.height

    // clamp your height to desired values
    if height > 90 {
        height = 90
    } else if height < 50 {
        height = 50
    }

    // update the constraint
    textViewHeightConstraint.constant = height
    self.view.layoutIfNeeded()
}

抱歉,我没有看到需要翻译的内容。请再次发送。
func textViewDidChange(_ textView: UITextView) {
    let maxHeight: CGFloat = 90.0
    let minHeight: CGFloat = 50.0
    textViewHeightConstraint.constant = min(maxHeight, max(minHeight, textView.contentSize.height))           
    self.view.layoutIfNeeded()
}

确保将您的控制器设置为textView的代理。在viewDidLoad中,textView.delegate = self或在storyboard中进行设置。 - pkorosec
同时,您的textView的高度不应受任何限制。垂直方向上,它只应该固定在底部,并从高度约束中获取其高度。 - pkorosec
在输入最大高度后,如果我输入文本,则不会显示。 - Midhun Narayan
请确保在TextView上启用滚动条,使其在最大高度后滚动。 - pkorosec
最佳答案是这个问题的解决方案。 - Nasir Khan

4
如果您正在子类化UITextView,这也是一个解决方案:
class Message: UITextView {


    var maximalSizeNotScrollable: CGFloat = 200
    var minimumHeightTextView: CGFloat = 35

    var textViewHeightAnchor: NSLayoutConstraint!


    override var contentSize: CGSize {
        didSet {

            if textViewHeightAnchor != nil {
                textViewHeightAnchor.isActive = false

            }
            textViewHeightAnchor = heightAnchor.constraint(equalToConstant: min(maximalSizeNotScrollable, max(minimumHeightTextView, contentSize.height)))
            textViewHeightAnchor.priority = UILayoutPriority(rawValue: 999)

            textViewHeightAnchor.isActive = true
            self.layoutIfNeeded()
        }
    }
}

优先级必须小于1000,否则您将遇到_UITemporaryLayoutHeight的问题。


2

我曾经也遇到过同样的问题。在阅读了@Anuraj的回答后,我将UITextView的高度约束拖放到了视图控制器中,因为当textview达到最大高度并启用滚动时,textview会变小以显示一行,所以我通过给textview的高度约束设置最大高度来解决了这个问题。Textview会扩展直到达到最大高度,之后它将可以滚动。当你删除文本时,textview的高度会自动变小。

@IBOutlet weak var txMessageHeight: NSLayoutConstraint!    
let textViewMaxHeight: CGFloat = 120

func textViewDidChange(_ textView: UITextView) {
    if textView.contentSize.height >= self.textViewMaxHeight{
        txMessageHeight.constant = self.textViewMaxHeight
        textView.isScrollEnabled = true
    }else{
        textView.frame.size.height = textView.contentSize.height
        textView.isScrollEnabled = false
    }
}

0
在StoryBoard中将TextView的高度约束设置为<=120(maxHeight)。

enter image description here

func textViewDidChange(_ textView: UITextView) {
        if textView.contentSize.height >= maxHeight {
            textView.isScrollEnabled = true
        } else {
            textView.isScrollEnabled = false
        }
    }

如果你想在 TextView 内容为空时重置其高度,只需要编写以下代码:

UIView.animate(withDuration: 0.15) {
   self.view.layoutIfNeeded()
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接