So I have a UITextView that the user can edit, but there may also be some text inserted programmatically into the textView as well, and it could be inserted at any arbitrary range in the textView. Currently, this also moves the caret position from where the user was currently working, which is undesirable. So anytime I do this:
self.textView.text = //some text inserted
It moves the caret position. So what would be the best way to prevent the caret position from moving, or make it totally ignorant and independent of programmatic changes and only move in response to user changes?
The best answer would be to set the text that is inserted like you have, but then after, set the caret position using the following method:
textView.selectedRange = NSMakeRange(0,0)
The first number is the spot in the text view you would like to put the caret. If you would like to keep it at the beginning regardless of whatever text is added, this is how you would do it. You would, however, want to include a conditional to keep this only around the non-typed text being added. When the user is typing, you want the caret to move.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With