I have an iOS application where user can order prints of photos. User can add text of his choice on to the photos before ordering the prints. Now, my problem is the printer we have do not support Chinese characters. Do I have a way in which I can restrict the user to use only a set of languages that my printer supports? I assume an application cannot force the language a user can select for his keyboard. So, I wanted to disallow invalid text on "shouldChangeCharacters" method of the UITextView. I know I can come up with a set of all characters that are supported/unsupported by my printer and user NSCharacterSet to restrict but are there any better ways of achieving this?
Using an NSCharacterSet is the correct way to do it. Something like this:
- (NSCharacterSet *)bannedCharacters {
static NSCharacterSet *bannedCharacters;
static dispatch_once_t once;
dispatch_once(&once, ^{
NSMutableCharacterSet *set = [[NSMutableCharacterSet alloc] init];
// Use lines like these to add the characters allowed by your printer.
[set addCharactersInRange:NSMakeRange(65, 26)];
[set addCharactersInString:@"0123456789"];
bannedCharacters = [set invertedSet];
});
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)replacement {
NSRange badRange = [replacement rangeOfCharacterFromSet:[self bannedCharacters]];
return badRange.location == NSNotFound;
}
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