Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UIKeyboardType based on text input

I'm trying to change the UIKeyboardType to the alphabet keyboard when the user types a space, mirroring the effect of typing an apostrophe. However, my code won't change the keyboard appearance until the user dismisses the keyboard and then brings it back again.

Edit: To clarify, the keyboard type starts as UIKeyboardTypeNumbersAndPunctuation and I want to change to ASCIICapable, because the typical user input is in the form of "# cups flour". I realized that the ASCIICapable keyboard has this functionality built-in, so presenting the ASCII capable keyboard but showing the numbers/punctuation first would work.

Here's my code:

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

    if ([string isEqualToString:@" "]) {
        textField.keyboardType = UIKeyboardTypeASCIICapable;
    }

return YES;
}
like image 745
MaxGabriel Avatar asked Jun 19 '12 02:06

MaxGabriel


1 Answers

dismiss the keyboard and then become the first responder again. In my code, I created a IBOutlet for the textfield *tf. It worked.

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

    if ([string isEqualToString:@" "]) {
        self.tf.keyboardType = UIKeyboardTypeNumberPad;
        [textField resignFirstResponder];
        [self.tf becomeFirstResponder];
    }

    return YES;
}
like image 128
Bruno Koga Avatar answered Sep 27 '22 22:09

Bruno Koga