文本跳转,同时迅速自动调整textView的大小

时间:2018-06-26 15:10:26

标签: ios swift uitextview

如果我的应用中有一个简单的messageView。 messageView上的是一个带有textView的输入容器。 textView应该根据其内容调整大小。

到目前为止,它仍然有效,但是每次将第一个字符的文本“跳转”到下一行时,都将第二个字符重新定位。看起来像: enter image description here

enter image description here

enter image description here

我的大部分代码。我认为这与textView(?)的滚动功能有关。

private let container: UIView = {
    let view = UIView()

    view.backgroundColor = UIColor.white
    view.layer.cornerRadius = 20
    view.layer.masksToBounds = true
    view.layer.borderColor = UIColor(red:0.90, green:0.90, blue:0.90, alpha:1.0).cgColor
    view.layer.borderWidth = 0.5
    view.translatesAutoresizingMaskIntoConstraints = false

    return view
}()

private lazy var inputTV: UITextView = {
    let tv = UITextView()

    tv.translatesAutoresizingMaskIntoConstraints = false
    tv.font = UIFont(name: "OpenSans-Light", size: 16)
    tv.backgroundColor = .red
    tv.delegate = self
    tv.textContainer.lineBreakMode = .byWordWrapping

    return tv
}()

override internal init(frame: CGRect) {
    super.init(frame: CGRect.zero)

    translatesAutoresizingMaskIntoConstraints = false

    addSubview(container)
    container.addSubview(inputTV)

    container.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -16).isActive = true
    container.leftAnchor.constraint(equalTo: leftAnchor , constant: 16).isActive = true
    container.rightAnchor.constraint(equalTo: rightAnchor, constant: -16).isActive = true
    containerHeightAnchor = container.heightAnchor.constraint(equalToConstant: 40)
    containerHeightAnchor?.isActive = true 

    inputTV.leftAnchor.constraint(equalTo: uploadButton.rightAnchor).isActive = true
    inputTV.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
    inputTV.rightAnchor.constraint(equalTo: sendButton.leftAnchor, constant: -5).isActive = true
    textViewHeightAnchor = inputTV.heightAnchor.constraint(equalTo: container.heightAnchor)
    textViewHeightAnchor?.isActive = true
}

internal func textViewDidChange(_ textView: UITextView) {
    let contentHeight = textView.contentSize.height
    containerHeightAnchor?.constant = max(contentHeight, 40)
    inputTV.frame.size.height = contentHeight
}

我希望有人能提供帮助。问候

1 个答案:

答案 0 :(得分:0)

由于@DonMag,我确实从heightAnchor中删除了textView,它确实对我有用。新代码如下:

  • 禁止在textView(isScrollEnabled = false)上滚动

    private lazy var inputTV: UITextView = {
        let tv = UITextView()
    
        tv.translatesAutoresizingMaskIntoConstraints = false
        tv.font = UIFont(name: "OpenSans-Light", size: 16)
        tv.delegate = self
        tv.isScrollEnabled = false
    
        return tv
    }()
    
  • 删除heightAnchor( inputTV.heightAnchor和我的textViewHeightAnchor

    override internal init(frame: CGRect) {
        super.init(frame: CGRect.zero)
    
        translatesAutoresizingMaskIntoConstraints = false
    
        addSubview(container)
        container.addSubview(inputTV)
    
        ...
    
        inputTV.leftAnchor.constraint(equalTo: uploadButton.rightAnchor).isActive = true
        inputTV.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
        inputTV.rightAnchor.constraint(equalTo: sendButton.leftAnchor, constant: -5).isActive = true
    }