美文网首页
对输入钱数的textField的设置

对输入钱数的textField的设置

作者: binkylee | 来源:发表于2016-08-26 11:09 被阅读0次

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

    {

    NSRange aRange = [textField.text rangeOfString:@"."];

    if (aRange.location != NSNotFound && [string isEqualToString:@"."]) {

    return NO;

    }

    NSMutableString * futureString = [NSMutableString stringWithString:textField.text];

    [futureString  insertString:string atIndex:range.location];

    NSInteger flag = 0;

    const NSInteger limited = 2;//小数点后需要限制的个数

    for (NSInteger i = futureString.length - 1; i>= 0; i--) {

    if ([futureString characterAtIndex:i] == '.') {

    if (flag > limited) {

    return NO;

    }

    break;

    }

    flag++;

    }

    return YES;

    }

    - (void) textFieldDidEndEditing:(UITextField *)textField{

    if (textField.text.length) {

    NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithString:textField.text];

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];

    numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;

    numberFormatter.currencySymbol = @"";

    textField.text = [numberFormatter stringFromNumber:decimalNumber];

    }

    }

    相关文章

      网友评论

          本文标题:对输入钱数的textField的设置

          本文链接:https://www.haomeiwen.com/subject/fcxzsttx.html