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
Another version of the one simeon posted (which was a version of Chaise's). This one works with text fields (UITextField
). You'll have to set up the UITextFieldDelegate
for this to do anything. I commented out the line for updating the caret, but it still seems to work.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)text
{
//Check if a space follows a space
if ( (range.location > 0 && [text length] > 0 &&
[[NSCharacterSet whitespaceCharacterSet] characterIsMember:[text characterAtIndex:0]] &&
[[NSCharacterSet whitespaceCharacterSet] characterIsMember:[[textField text] characterAtIndex:range.location - 1]]) )
{
//Manually replace the space with your own space, programmatically
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@" "];
//Make sure you update the text caret to reflect the programmatic change to the text view
// textField.selectedTextRange = NSMakeRange(range.location+1, 0);
//Tell Cocoa not to insert its space, because you've just inserted your own
return NO;
}
return YES;
}