A growing view of text input - quick examples

I have been trying for the last two days to implement a UITextView, which grows as the user types. Like the whats app.

I found examples here with textViewDidChange, but it really did not work, since you need to not only grow, but also move up. And since the text view is inside the view, which also contains the submit button, they should both grow up.

Also found are several other frameworks.

https://github.com/slackhq/SlackTextViewController - it looks really cool and can do everything I need, but I did not find examples of its quick launch. An example project where I could not work.

https://github.com/MatejBalantic/MBAutoGrowingTextView - could not get it to work up.

I was looking for some help on how to implement this with SWIFT, or perhaps a sample SLACK project running on a fast one, which is not a sample project on github. Or maybe a little code showing how to associate a UITextView with a Slack class and make it work inside the viewController class.

+4
source share
3 answers

Here is what I am currently using for my inputAccessoryView.

I have a toolbar that contains a UITextView and send button and listens for textViewDidChange events as follows:

func textViewDidChange(_ textView: UITextView) {
    let oldHeight = textView.height
    let maxHeight: CGFloat = 100.0 //beyond this value the textView will scroll
    var newHeight = min(textView.sizeThatFits(CGSize(width: textView.frame.width, height: CGFloat.greatestFiniteMagnitude)).height, maxHeight)
    newHeight = ceil(newHeight)
    if newHeight != oldHeight {
        textView.frame.size.height = max(newHeight, barHeight)
        updateHeight(height: newHeight)
    }
}

- textView.

public func updateToolBarHeight(height: CGFloat) {
    for constraint in constraints {
        if constraint.firstAttribute == NSLayoutAttribute.height && constraint.firstItem as! NSObject == self {
            constraint.constant = max(barHeight, height)
        }
    }
    setNeedsUpdateConstraints()
    sendButton.origin.y = max(0, height - barHeight)
}

barHeight - ( ​​ 50.0). sendButton, .

+1

UITextView, , :

textView.isScrollEnabled = false

.

0

All you have to do to make the UITextView grow automatically is to set numberOfLines to 0. Then it will grow indefinitely. I hope this works for you!

-4
source

Source: https://habr.com/ru/post/1607885/


All Articles