only character - not special/numbers from keyboard - iPhone
-
16-09-2019 - |
Question
Normal UITextField
displays a UIKeyboard
which includes alpha+Numberic+specials
But I need alpha only...
I need this because if user enters something like -- A'bad
it has a char ' -- which is affecting my query .
How we can set only alphabetic
UIKeyboard
to user?
Solution
This SO post answers your question: UIkeyboard type
OTHER TIPS
I think. I got the answer.
In your UIViewController
file .h add delegate<UITextfieldDelegate>
Now add following code to .m file
- (BOOL) textField:(UITextField*)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString*)textEntered {
for (int i = 0; i < [textEntered length]; i++) {
unichar c = [textEntered characterAtIndex:i];
if (![myCharSet characterIsMember:c]) {
return NO;
}
}
return YES;
}
You should use as i do below;
- (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
{
NSMutableCharacterSet *filteredChars = [NSMutableCharacterSet letterCharacterSet];
[filteredChars formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
NSCharacterSet *blockedCharSet = [filteredChars invertedSet];
if (([string rangeOfCharacterFromSet:blockedCharSet].location == NSNotFound)) {
return YES;
}
return NO;
}
Instead of disabling or not allowing user to add '(apostorpe) use following code, it will allow you to insert '(apostorpe)
[textEntered stringByReplacingOccurrencesOfString:@" ' " withString:@" ' '"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow