views:

129

answers:

1

I have UITextFields on a table to enter values. Some of these fields accept only numbers. I am using UIKeyboardTypeNumbersAndPunctuation for the keyboardType, and shouldChangeCharactersInRange to filter the characters.

Also, all the corrections are disabled:

textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
textField.autocorrectionType =  UITextAutocorrectionTypeNo;
textField.autocapitalizationType =  UITextAutocapitalizationTypeNone;

On the numeric only fields, when the space key is touched, the keyboard changes to Alphabet. I know that this is the default behavior. I want to ignore the space key, and don't want the keyboard type change.

Is there some way to change this default behavior?

PS: the other numeric keyboard types are not an option. I need the Punctuation!

Thanks

+1  A: 

I do think it is possible to modify the keyboard behavior.

However, you could implement the textField:shouldChangeCharactersInRange:replacementString: from the UITextFieldDelegate protocol to intercept the space (and apostrophe) like this and it seems to work:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@" "] || [string isEqualToString:@"'"]) {
        NSMutableString *updatedString = [NSMutableString stringWithString:textField.text];
        [updatedString insertString:string atIndex:range.location];
        textField.text = updatedString;
        return NO;
    } else {
        return YES;
    }
}
gerry3