Reputation: 3087
I want my Keyboard to change from UIKeyboardTypeNumbersAndPunctuation mode to normal text mode after the user hits space.
So he can easily type something like "34 loops".
Upvotes: 0
Views: 955
Reputation: 12254
Okay, you cannot get direct input notifications, but there's a solution I can think of.
Implement the following delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
The replacement string will, as its name states, contain the string that wants to replace the existing string in the text field. What you could do is check the last character of this string, and if it is indeed a space, then change the keyboard type:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
char lastCharacter = [string characterAtIndex:[string length] - 1]; //Get the last input character in the ext field.
if(lastCharacter == ' ')
{
//The last character is a space, so...
textField.keyboardType = UIKeyboardTypeDefault;
[textField resignFirstResponder];
[textField becomeFirstResponder];
}
return YES;
}
Upvotes: 3
Reputation: 318774
The answer given by Leonnears mostly answers the simple case, but there are many issues you need to consider. What happens if the user types 34
, then a space, then delete? What happens when the user moves the caret from the end of loops
to somewhere in the number part?
It all starts to get more difficult to cover each case. But more importantly, it starts to get annoying for the user as the keyboard starts changing on them. It makes it very difficult to just type in the text. I've never seen an app do what you propose and there is a good reason.
Let the user use the normal keyboard like they are used to. Everyone knows how to switch between letter and numbers. Having the keyboard automatically change will be unusual and confusing.
Upvotes: 1