我将如何在文本字段中设置货币以将其显示为本地化货币,以 0 开头。如果有人输入 16.25 便士,它将分别格式化为 0.1625£。我正在使用委托并格式化所有文本字段,因此只能传入数字,该字段也应该本地化。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // First, create the text that will end up in the input field if you'll return YES:
NSString *resultString = [textField.text stringByReplacingCharactersInRange:range withString:string];
// Now, validate the text and return NO if you don't like what it'll contain.
// You accomplish this by trying to convert it to a number and see if that worked.
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber* resultingNumber = [numberFormatter numberFromString:resultString];
//[numberFormatter release];
return resultingNumber != nil;
我不希望这改变,因为它格式化了我所有的字段。只希望 textField1 具有相关格式,我将如何执行此操作,我认为它在于 viewdidload 方法并将文本属性设置为本地化为浮点,但我似乎无法弄清楚如何去做。