I have a library I’m building that needs to keep track of text entry into a UITextView in order to keep track of positions of certain sections of text. Most cases are handled fine within the textView(_:shouldChangeTextIn:replacementText:)
method. The problem I’m having is that certain keyboards have character combinations that will actually change the character without calling the textView(_:shouldChangeTextIn:replacementText:)
method. A simple example of this is if you choose a Vietnamese keyboard and type uw
it will change to ú
but the only calls to textView(_:shouldChangeTextIn:replacementText:)
will be for the entry of u and w. The offset calculations will be incorrect at this point. As far as I can tell there’s no other method that I could use to know about this change. I’m not sure if this is a bug or not. The textViewDidChange method will be called but I was hoping to avoid hacky logic where I compare the previous and new text of the textView to determine any changes and adjust logic there. If anyone has suggestions I would love to hear them. Hopefully someone has seen and solved this problem. Thanks.
To clarify, I need to handle this situation using something other than subclassing.
Sample project: https://github.com/szweier/ShouldChangeTextInRange
If you run the code above and choose the Vietnamese keyboard to type in uw
you will see that the string generated in the textView(_:shouldChangeTextIn:replacementText:)
method will be different from the text within the textView.