I am developing a richtext editor using UITextView, and I found a BUG of UITextViewDelegate's method:
optional func textView(
_ textView: UITextView,
shouldChangeTextIn range: NSRange,
replacementText text: String
) -> Bool
This BUG occurs when user tries to delete a selected text. For example:
When user deleting "llo" in "hello" by
select "llo"
press delete
The correct range should be NSRange(2, 3),but the actual range is NSRange(1, 4),and replacementText is a empty string. Which means it wants to replace "ello" with "" and it's not right.
The final result of this action is "llo" gets replaced by "", which is correct and corrupted with delegate method's range info! No wonder that we can't find this BUG until we test the delegate method.
However, when trying to replace "llo" with some text(NOT DELETING), the range info is correct.
In conculsion, the caller of the delegate method compute the range wrong when user try to delete a selected text.