views:

347

answers:

3

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 there any way to disable this shortcut in code?

Update: Disabling autocorrection via UITextInputTraits doesn't work.

Update 2: Got it! See my post below.

A: 

I don't think there's a way to turn off exactly this, but check out UITextInputTraits. This lets you declare properties of your field, for example you can say if it's for entering a URL. That influences the behavior of the keyboard. If the reason you don't want double space to yield a period and space is that the text should be literally what the user entered for some reason, perhaps you want to turn off autocorrection. It's possible turning off autocorrection turns off double space for period, I don't know.

Ken
A: 

OK, I figured it out. In your UITextView delegate, add this:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if([text isEqualToString:@". "])
        return NO;
}
igul222
Doesn't seem to work when using a `UITextField` :( `textField:shouldChangeCharactersInRange:replacementString:` isn't fired for this change...
Douwe Maan
A: 

Put this in your delegate class:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

 //Check for double space
 return !(range.location > 0 && 
          [string length] > 0 &&
          [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[string characterAtIndex:0]] &&
          [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[[textField text] characterAtIndex:range.location - 1]]);

}
Chaise