I have a UITextField in which you can enter only numbers and period “,”
This is how it’s made:
#define NUMBERS @"0123456789"
#define NUMBERSPERIOD @"0123456789,"
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *cs;
NSString *filtered;
// Check for period
if ([textFieled.text rangeOfString:@","].location == NSNotFound) {
cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERSPERIOD] invertedSet];
filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
// Period is in use
cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
Now I want to limit the digits entered after “,” to just 2. I’m not sure how to do this?
1 Answer