@@ -6,11 +6,36 @@ import Foundation
66import UIKit
77
88class AttachmentTextView : MentionTextView {
9- // When creating new lines, contentOffset is animated, but because
10- // we are simultaneously resizing the text view, this can cause the
11- // text in the textview to be "too high" in the text view.
12- // Solution is to disable animation for setting content offset.
9+
10+ private var textIsChanging = false
11+
12+ override func textView( _ textView: UITextView , shouldChangeTextIn range: NSRange , replacementText text: String ) -> Bool {
13+ textIsChanging = true
14+ return super. textView ( self , shouldChangeTextIn: range, replacementText: text)
15+ }
16+
17+ override func textViewDidChange( _ textView: UITextView ) {
18+ super. textViewDidChange ( textView)
19+ textIsChanging = false
20+ }
21+
1322 override func setContentOffset( _ contentOffset: CGPoint , animated: Bool ) {
14- super. setContentOffset ( contentOffset, animated: false )
23+ // When creating new lines, contentOffset is animated, but because because
24+ // we are simultaneously resizing the text view, on pre-iOS 13 this can
25+ // cause the text in the textview to be "too high" in the text view.
26+ // Solution is to disable animation for setting content offset between
27+ // -textViewShouldChange... and -textViewDidChange.
28+ //
29+ // We can't unilaterally disable *all* animated scrolling because that breaks
30+ // manipulation of the cursor in scrollable text. Animation is required to
31+ // slow the text view scrolling down to human scale when the cursor reaches
32+ // the top or bottom edge.
33+ let shouldAnimate : Bool
34+ if #available( iOS 13 , * ) {
35+ shouldAnimate = animated
36+ } else {
37+ shouldAnimate = animated && !textIsChanging
38+ }
39+ super. setContentOffset ( contentOffset, animated: shouldAnimate)
1540 }
1641}
0 commit comments