UITextViewDelegate behavior when backspace key is enabled HELD Down

I ran into a problem when iOS gives incorrect UITextViewDelegate information when the delete key is held on the keyboard.

When a HOLDS user deletes a key in a UITextView on an iPad, a UITextView will begin to delete whole words instead of single characters, the longer it is held (note: this does not happen in the simulator).

When this happens, the UITextView delegation method:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 

Gets a call with a range consisting of the correct cursor location, but with a length of 1. This is not true since UITextView now removes whole words, not single letters. The following code, for example, will print only one space.

 [textView substringWithRange:range] string contains " " 

Although UITextView removes the whole word. Replaceable text is correctly set as an empty string. Does anyone know of a solution or workaround for this problem?

+4
source share
1 answer

Jacob mentioned that I should post this as an answer. So there it is.

My workaround for this is to track the length and range of the text specified in the shouldChangeTextInRange file, and then compare it with the length of the text in textViewDidChange. If the differences are not synchronized, I clear my backup buffer and restore it from the text view. This is not optimal. Here is my 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]; } } 
+4
source

All Articles