By default, if you tap the spacebar twice on the iPhone or iPad, instead of getting \" \" (two spaces), you get \". \" (a period followed by a space). Is the
A simpler solution that I have found to work for a UITextView
is as follows:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@". "] && range.length == 1) {
NSMutableAttributedString *attributedString = [textView.attributedText mutableCopy];
[attributedString replaceCharactersInRange:range withString:@" "];
textView.attributedText = attributedString;
textView.selectedRange = NSMakeRange(range.location + 1, 0);
return NO;
}
return YES;
}
This allows multiple spaces to be entered repeatedly and also handles attributed text. The only case I have found it to fail with is when pasting a period and space with a single character already selected.