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.
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
}
}
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)
})
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.