Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does shouldChangeTextInRange get called multiple times using predictive input?

The predictive-input of iOS8 calls the following delegate method of UITextView multiple times resulting in the selected word being inserted multiple times into the view.

This code works for typing single letters and copy/paste but not when using the predictive-input bar; why not?

- (BOOL) textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    textView.text = [textView.text stringByReplacingCharactersInRange:range withString:text];
    return false;
}

With this code; if I enter an empty UITextView and tap on "The" in the predictive text (autocomplete) view it inserts "The The" into the view by way of making three calls on this method. The parameters passed in for each call are:

  • range : {0,0} text : @"The"
  • range : {0,0} text : @"The"
  • range : {3,0} text : @" "

The space I can understand; but why insert "The" twice?

like image 462
Wex Avatar asked Oct 21 '14 08:10

Wex


1 Answers

I got this same issue. It appears that with predictive text, setting textView.text in that delegate method triggers an immediate call to that delegate method again (this only happens with predictive text as far as I know).

I fixed it by just surrounding my textView changes with a guard:

private var hack_shouldIgnorePredictiveInput = false

func textView(textView: UITextView!, shouldChangeTextInRange range: NSRange, replacementText text: String!) -> Bool {
    if hack_shouldIgnorePredictiveInput {
        hack_shouldIgnorePredictiveInput = false
        return false
    }

    hack_shouldIgnorePredictiveInput = true

    textView.text = "" // Modify text however you need. This will cause shouldChangeTextInRange to be called again, but it will be ignored thanks to hack_shouldIgnorePredictiveInput

    hack_shouldIgnorePredictiveInput = false

    return false
}
like image 101
Richard Venable Avatar answered Sep 19 '22 13:09

Richard Venable