andre_hold
andre_hold

Reputation: 592

Text jumps while automatically resize textView in swift

if have a simple messageView in my app. on the messageView is a input-container with a textView. The textView should resize depending on its content.

It works so far, but every time while wrapping into the next line the text "jumps" for the first character, but reposition with the second character. it looks like: enter image description here

enter image description here

enter image description here

the most of my code. i assume that it has something to do with the scroll capabilities of the 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
}

I hope somebody could help. regards

Upvotes: 2

Views: 647

Answers (1)

andre_hold
andre_hold

Reputation: 592

thanks to @DonMag i do remove the heightAnchor from textViewand it does work for me. The new code is as following:

  • disable scrolling on the 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
    }()
    
  • remove heightAnchor (no inputTV.heightAnchor resp. my 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
    }
    

Upvotes: 1

Related Questions