ITextField使其表现得像仅适用于iOS中数字输入的货币计

ITextField使其表现得像仅适用于iOS中数字输入的货币计

本文介绍了尝试格式化UITextField使其表现得像仅适用于iOS中数字输入的货币计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个 UITextField ,应该只接收来自用户的数字输入。此数字输入应该代表货币(即,只有两位小数),当未输入任何内容时,其默认值为 0.00 。我还想在最左侧有一个 $ 符号,数字右对齐,这样数字从右向左移动,如下所示:

I have a UITextField in my application that is supposed to receive only numeric input from the user. This numeric input is supposed to represent currency (i.e. have only two decimal places), have the default value of 0.00 when nothing has been entered. I also would like to have a "$" sign that is on the far left, with the number right aligned, such that the number moves from right to left like such:

例如输入数字 1234.56 时。

1。 $ 0.00

2。 $ 0.01

3。 $ 0.12

4。 $ 1.23

5。 $ 12.34

6。 $ 123.45

7。 $ 1,234.56

8。依此类推...

我将 UITextField 右对齐,以便它已经移动了数字从右到左。但是,我需要将 $ 放在 UITextField 内,并且我希望将默认的 0.00 值修改为输入的数字由用户一次一位。用户不必输入小数点,因为它应该已经在保持其位置的文本字段中。然后,我希望该数字具有一个,以便每三位数字分开输入。我希望应用程序在用户输入数字时执行此操作,并且在输入所有内容后不格式化。我该怎么做?我已经看到很多人说我要使用 NSNumberFormatter ,但是我想知道如何将其与 UITextField 这样就可以动态设置数字文本的格式,如上所述?

I have the UITextField right aligned so that it already moves the numbers over to the left from the right. However, I need to place the "$" inside the UITextField, and I would like the default 0.00 value to be modified to the number entered by the user one digit at a time. The user should NOT have to enter the decimal point, as it should already be in the textfield holding its position. I then would like the number to have a "," to be automatically entered to separate every three digits. I would like the application to do this AS THE USER IS ENTERING THE NUMBERS, and not formatted afterwards when everything is entered. How would I do this? I have seen a lot of people say that I am to use NSNumberFormatter, but I want to know how do I use it in conjunction with the UITextField such that it formats the numeric text on the fly, as described above?

我前一段时间在StackOverflow上发现了以下问题:

I have found the following question posted on StackOverflow some time ago:

,但我想知道如何将其作为解决方案。

but I want to know how to actually incorporate it as a solution to my problem.

推荐答案

这是一种很好的解决方案。记住要在viewDidLoad方法中将UITextField设置为self,并且头文件必须符合UITextFieldDelegate协议

Here's a nice way to do it. Remember to set your UITextField to self in the viewDidLoad method and your header file must conform to the UITextFieldDelegate protocol

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

    NSString *cleanCentString = [[textField.text componentsSeparatedByCharactersInSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
    NSInteger centValue = [cleanCentString intValue];
    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    NSNumber *myNumber = [f numberFromString:cleanCentString];
    NSNumber *result;

    if([textField.text length] < 16){
        if (string.length > 0)
        {
            centValue = centValue * 10 + [string intValue];
            double intermediate = [myNumber doubleValue] * 10 +  [[f numberFromString:string] doubleValue];
           result = [[NSNumber alloc] initWithDouble:intermediate];
        }
        else
        {
            centValue = centValue / 10;
            double intermediate = [myNumber doubleValue]/10;
            result = [[NSNumber alloc] initWithDouble:intermediate];
        }

        myNumber = result;
         NSLog(@"%ld ++++ %@", (long)centValue, myNumber);
            NSNumber *formatedValue;
            formatedValue = [[NSNumber alloc] initWithDouble:[myNumber doubleValue]/ 100.0f];
            NSNumberFormatter *_currencyFormatter = [[NSNumberFormatter alloc] init];
            [_currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
            textField.text = [_currencyFormatter stringFromNumber:formatedValue];
            return NO;
    }else{

        NSNumberFormatter *_currencyFormatter = [[NSNumberFormatter alloc] init];
        [_currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
        textField.text = [_currencyFormatter stringFromNumber:00];

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Deposit Amount Limit"
                                                       message: @"You've exceeded the deposit amount limit. Kindly re-input amount"
                                                      delegate: self
                                             cancelButtonTitle:@"Cancel"
                                             otherButtonTitles:@"OK",nil];

        [alert show];
        return NO;
    }
    return YES;
}

这篇关于尝试格式化UITextField使其表现得像仅适用于iOS中数字输入的货币计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:26