切换启用UITextView在最大行数后滚动。

问题描述 投票:0回答:1

我有一个 uitextview subview 的输入容器视图中,因为它可能出现在一个消息应用程序中。

从程序上讲,在加载时,输入容器View的高度锚定器将被设置为 uitextview 是在初始化 inputContainerView 时设置的。当输入更多的文本行时,这个约束可以很好地增加容器的高度。

我的目标是将输入框的高度限制在 uitextview 在达到X行数后,此后输入的任何行数都是可以滚动的。而同样的,当行数低于最大行数时,它就会恢复到原来的形式--不可滚动,并根据内容自动调整高度。

经过多次试验和研究,我已经成功地让高度固定在达到最大行数后,但我似乎无法解决如何在行数低于最大行数时将其恢复到原始形式。看来 uitextview 高度停留在行数最大的高度上。

下面是相关代码。

//Container View Initialization, onLoad Frame Height is set to 60
override init(frame: CGRect) {
super.init(frame: frame)
autoresizingMask = .flexibleHeight

addSubview(chatTextView)
textView.heightAnchor.constraint(greaterThanOrEqualToConstant: frame.height - 20).isActive = true
}

//TextView Delegate

var isTextViewOverMaxHeight = false
var textViewMaxHeight: CGFloat = 0.0

func textViewDidChange(_ textView: UITextView) {
    let numberOfLines = textView.contentSize.height/(textView.font?.lineHeight)!

    if Int(numberOfLines) > 5 {

        if !isTextViewOverMaxHeight {
            containerView.textView.heightAnchor.constraint(greaterThanOrEqualToConstant: 40).isActive = false
            containerView.textView.heightAnchor.constraint(equalToConstant: 
            containerView.textView.frame.height).isActive = true
            containerView.textView.isScrollEnabled = true                

            textViewMaxHeight = containerView.textView.frame.height
            isTextViewOverMaxHeight = true
        }
    } else {

        if isTextViewOverMaxHeight {
            containerView.textView.heightAnchor.constraint(equalToConstant: textViewMaxHeight).isActive = false
            containerView.textView.heightAnchor.constraint(greaterThanOrEqualToConstant: 40).isActive = true
            containerView.textView.isScrollEnabled = false                

            isTextViewOverMaxHeight = false
        }
    }
}
ios swift uitextview nslayoutconstraint uitextviewdelegate
1个回答
1
投票

你把眼下一个简单的问题搞得过于复杂了,下面是如何实现你想要的东西

class ViewController: UIViewController {
    var heightConstraint: NSLayoutConstraint!
    var heightFor5Lines: CGFloat = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        let textView = UITextView(frame: CGRect.zero)
        textView.delegate = self
        textView.backgroundColor = UIColor.red
        textView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(textView)
        textView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        textView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        textView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
        heightConstraint = textView.heightAnchor.constraint(equalToConstant: 30.0)
        heightConstraint.isActive = true
    }
}


extension ViewController: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        let numberOfLines = textView.contentSize.height/(textView.font?.lineHeight)!

        if Int(numberOfLines) > 5 {
            self.heightConstraint.constant = heightFor5Lines
        } else {
            if Int(numberOfLines) == 5 {
                self.heightFor5Lines = textView.contentSize.height
            }
            self.heightConstraint.constant = textView.contentSize.height
        }
        textView.layoutIfNeeded()
    }
}

enter image description here

这是如何工作的?

很简单,你的textView一开始就有一定的高度(这里的高度约束是必要的,因为我既没有禁止滚动,也没有提供底部约束,所以它没有足够的数据来评估它的内在高度),每当文本变化时,你就检查行数。只要行数小于阈值行数,你就不断增加文本视图的高度限制,使其框架与内容大小相匹配(因此不会滚动),一旦达到预期行数,你就限制高度,使文本视图框架小于实际内容大小,从而自动滚动。

希望能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.