Setting UITextView text color after text change causes caret rect to jump up and down

It seems that when typing inside a textview that has insets and padding, the caret keeps jumping up and down on most keystrokes. Is there a solution to this?

class ViewController: UIViewController, UITextViewDelegate {

    @IBOutlet weak var textView: TextView!

    override func viewDidLayoutSubviews() {
        let h = textView.bounds.size.height / 2
        textView.textContainerInset = UIEdgeInsets(top: h, left: 0, bottom: h, right: 0)
        textView.textContainer.lineFragmentPadding = 200
    }

    func textViewDidChange(_ textView: UITextView) {
        textView.textStorage.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: 1))
    }

}

class TextView: UITextView {

    override func caretRect(for position: UITextPosition) -> CGRect {
        let r = super.caretRect(for: position)
        print(r)
        return r
    }

}

A complete project can be found here: https://www.icloud.com/iclouddrive/0yEBQZPUCZQH1o3HiL8_6q_gQ#problem_copia

Setting UITextView text color after text change causes caret rect to jump up and down
 
 
Q