且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在UITextfield中使用NSNumberFormatter实时格式化

更新时间:2023-11-10 09:32:10

存储字符串值,附加到它,然后用于操作。

Store the string value, append to it, then use that for the operation.

定义NSMutableString .h文件

Define an NSMutableString in the .h file

NSMutableString *storedValue;
@property (nonatomic, retain) NSMutableString *storedValue;

合成。

...

- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   if ([textField tag] == amountTag)
    {
    [storedValue appendString:string];
    NSString *newAmount = [self formatCurrencyValue:([storedValue doubleValue]/100)];

    [textField setText:[NSString stringWithFormat:@"%@",newAmount]];
    return NO;
    }

    //Returning yes allows the entered chars to be processed
    return YES;
}