1

I know that there is automatic scrolling when the content(text) exceeds the default height of the text view, but I want the text view to be full screen and when the content comes to the keyboard level, the text view should scroll automatically. I have given a GIF to help readers understand better.

Notes on IOS

class StartStoryPopUp: UIViewController, UITextViewDelegate {
    // Setup text view
    func setupTextView() {

        textViewOne.textColor = .black
        textViewOne.backgroundColor = baseColor
        // Add some padding to the text insde the text view
        textViewOne.textContainerInset = UIEdgeInsets(top: 15, left: 10, bottom: 15, right: 10)
        textViewOne.font = UIFont(name: "PatrickHand-Regular", size: 25)
        textViewOne.layer.cornerRadius = 25

        popUp.addSubview(textViewOne)
        addTextViewConstraints()
    }

    // Add the constraints to the 'start story' text view
    func addTextViewConstraints() {

        textViewOne.translatesAutoresizingMaskIntoConstraints = false
        textViewOne.leadingAnchor.constraint(equalTo: popUp.leadingAnchor, constant: 3).isActive = true
        textViewOne.trailingAnchor.constraint(equalTo: popUp.trailingAnchor, constant: -3).isActive = true
        textViewOne.topAnchor.constraint(equalTo: popUp.topAnchor, constant: 3).isActive = true
        textViewOne.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -30).isActive = true
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Abdullah Ajmal
  • 431
  • 5
  • 17

1 Answers1

3

Swift 5

Maybe this lines will work for you.

You should set contentOffSet in textViewDidChange function with animation like this:

func textViewDidChange(_ textView: UITextView) {

    let line = textView.caretRect(for: (textView.selectedTextRange?.start)!)
    let overFlow = line.origin.y + line.size.height - (textView.contentOffset.y + textView.bounds.size.height - textView.contentInset.bottom - textView.contentInset.top)

    if 0 < overFlow {

        var offSet = textView.contentOffset
        offSet.y += (overFlow + 7)

        UIView.animate(withDuration: 0.3, animations: {
            textView.setContentOffset(offSet, animated: true)
        })
    }
}
Habin Lama
  • 529
  • 5
  • 19
emrcftci
  • 3,355
  • 3
  • 21
  • 35