Normally, changing the font of a UITextView is done like this:
textView.font = .systemFont(ofSize: 20)
This is fine if done before the text is loaded or right at the start of a long scrollable text view. However, if the font size is changed in the middle or end of a long text view, when scrolled to top, the text view leaves a huge chunk of empty space and forces an unpleasant resize.
Is there a way to allow a smooth change? How did Apple do it in its Books app?
Full code to reproduce this behavior:
import UIKit
class ViewController: UIViewController {
var textView: UITextView!
@objc func handleChange() {
print("Clicked!")
textView.font = .systemFont(ofSize: 20)
}
override func viewDidLoad() {
super.viewDidLoad()
textView = UITextView()
self.title = "TextView"
let change = UIBarButtonItem(title: "change", style: .plain, target: self, action: #selector(handleChange))
self.navigationItem.rightBarButtonItem = change
textView.font = .systemFont(ofSize: 40)
textView.frame = view.bounds
view.addSubview(textView)
textView.backgroundColor = .red
}
}