自适应 uitextview 直到特定高度

Self sizing uitextview till specific height

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

我怎样才能做到这一点?

禁用 textView 的滚动。

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

提供最大高度限制 然后将此代码添加到您的 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
            }
        }
    }

为您的 textView 添加一个高度限制并创建一个出口以便您可以对其进行调整。然后就可以使用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()
}

如果您是 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 的问题。

我也在为同样的问题苦苦挣扎。阅读@Anuraj 的回复后,我将 UITextView 的高度约束拖放到视图控制器,因为当 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
    }
}

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

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()
}