I've run into a problem where iOS is giving my UITextViewDelegate incorrect information when the delete key is held on the keyboard.
When the user HOLDS the delete key on a UITextView on an iPad the UITextView will begin to delete entire words instead of individual characters the longer it is held down (note: this does not occur in the simulator).
When this happens, the UITextView delegate method:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
Gets called with a range consisting of the correct cursor location, but a length of 1. This is incorrect as the UITextView is now deleting entire words, not single letters. The following code, for example, will print only a single space.
[textView substringWithRange:range]
string contains " "
Despite the UITextView removing a whole word. The replacement text is correctly given as the empty string. Does any one know of a solution or workaround to this problem?
Jacob mentioned I should post this as an answer. So here it is.
My hackish workaround to this is to monitor the text length and range given in shouldChangeTextInRange, and then compare it to the length of the text in textViewDidChange. If the differences are out of sync I flush my backing text buffer and rebuild it from the text view. This is not optimal. Here is my temporary workaround:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//Push the proposed edit to the underlying buffer
[self.editor.buffer changeTextInRange:range replacementText:text];
//lastTextLength is an NSUInteger recording the length that
//this proposed edit SHOULD make the text view have
lastTextLength = [textView.text length] + ([text length] - range.length);
return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{
//Check if the lastTextLength and actual text length went out of sync
if( lastTextLength != [textView.text length] )
{
//Flush your internal buffer
[self.editor.buffer loadText:textView.text];
}
}
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