UITextView: autocorrect ignores textView:shouldChangeTextInRange:replacementText: returning NO

Originator:james
Number:rdar://15805525 Date Originated:1/13/14
Status:Open Resolved:
Product:iOS SDK Product Version:
Classification: Reproducible:
 
Sample code: https://www.dropbox.com/s/1q3vqfnsmmbhnuj/AutocorrectBug.zip

Summary:
I'm able to reproduce this behavior on my device (iPad retina) and on the simulator only if I use the software keyboard (i.e. use my mouse to click the on-screen keyboard).

Consider the following UITextViewDelegate method:

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

    if ([text isEqualToString:@" "]
        && range.length == 0
        && (range.location == 0
            || [[textView.text substringWithRange:NSMakeRange(range.location-1, 1)] isEqualToString:@"\n"])) {
                textView.backgroundColor = [UIColor yellowColor];
                return NO;
    }
    return YES;
}

My intention with this code is that when the user types a space, and that space is the first character on the line, the space is ignored and instead some other functionality happens, in this case the UITextView turns yellow.

What appears to be an autocorrection bug ignores my attempt to disregard the space, and presents autocorrection options with a leading space.

Steps to Reproduce:
Consider the attached sample project.

1. Tap in the text view to make it first responder
2. Type "Apple", hit return and note the positioning
3. Now, type a space (turning the text view yellow), type "Apple" again and hit return.

Expected Results:
Expected: A yellow background and text reading:

Apple
Apple

(no space before either word)

Actual Results:
Observed: A yellow background and text reading (due to an autocorrection):

Apple
 Apple

(space before the second "Apple")

Version:
iOS iPad simulator 7.0.3
iPad MD328LL/A - 7.0.2 (11A501)

Notes:


Configuration:
Does not occur if using the simulator and a hardware keyboard, must click on software keyboard in simulator.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!