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;
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With