0

数字とピリオドのみを入力できるUITextFieldがあります"、"これが作成方法です。

#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];

}

ここで、「、」の後に入力する桁を2桁に制限したいと思います。これを行う方法がわかりませんか?

4

1 に答える 1

0
int positionOfPeriod = [filtered rangeOfString:@","].location;
filtered = [filtered substringToIndex: MIN(positionOfPeriod+3, [filtered length])];
于 2012-06-28T13:42:54.887 に答える