From 4e976971884cfcf8819ff858a876f10cd02cc604 Mon Sep 17 00:00:00 2001 From: eminbugrasaral Date: Tue, 13 May 2014 15:46:42 +0300 Subject: [PATCH] Replace a currency's prefix with its suffix if it is a curreny with suffix such as TRY --- FormattedCurrencyInput/ViewController.m | 67 +++++++++++++++---------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/FormattedCurrencyInput/ViewController.m b/FormattedCurrencyInput/ViewController.m index fa54669..a482b18 100644 --- a/FormattedCurrencyInput/ViewController.m +++ b/FormattedCurrencyInput/ViewController.m @@ -19,13 +19,17 @@ @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; - _textField.delegate = self; - + _textField.delegate = self; + NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; [numberFormatter setMaximumFractionDigits:2]; [numberFormatter setMinimumFractionDigits:2]; - + + NSString *currencySymbol = [[numberFormatter currencySymbol] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + [numberFormatter setPositivePrefix:currencySymbol]; + [numberFormatter setPositiveSuffix:@""]; + _textField.text = [numberFormatter stringFromNumber:[NSNumber numberWithInt:0]]; } @@ -42,24 +46,28 @@ - (void)didReceiveMemoryWarning - (IBAction)valueButton:(id)sender { NSString *textFieldStr = [NSString stringWithFormat:@"%@", _textField.text]; - + NSMutableString *textFieldStrValue = [NSMutableString stringWithString:textFieldStr]; - + NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; - [textFieldStrValue replaceOccurrencesOfString:numberFormatter.currencySymbol + NSString *currencySymbol = [[numberFormatter currencySymbol] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + [numberFormatter setPositivePrefix:currencySymbol]; + [numberFormatter setPositiveSuffix:@""]; + + [textFieldStrValue replaceOccurrencesOfString:currencySymbol withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [textFieldStrValue length])]; - + [textFieldStrValue replaceOccurrencesOfString:numberFormatter.groupingSeparator withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [textFieldStrValue length])]; NSDecimalNumber *textFieldNum = [NSDecimalNumber decimalNumberWithString:textFieldStrValue]; - + UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"Value: %@, Number: %@", textFieldStr, textFieldNum] delegate:nil @@ -71,63 +79,68 @@ - (IBAction)valueButton:(id)sender - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string { NSInteger MAX_DIGITS = 11; // $999,999,999.99 - + NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; [numberFormatter setMaximumFractionDigits:2]; [numberFormatter setMinimumFractionDigits:2]; - + + // Set currency symbol as prefix + NSString *currencySymbol = [[numberFormatter currencySymbol] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + [numberFormatter setPositivePrefix:currencySymbol]; + [numberFormatter setPositiveSuffix:@""]; + NSString *stringMaybeChanged = [NSString stringWithString:string]; if (stringMaybeChanged.length > 1) { NSMutableString *stringPasted = [NSMutableString stringWithString:stringMaybeChanged]; - - [stringPasted replaceOccurrencesOfString:numberFormatter.currencySymbol + + [stringPasted replaceOccurrencesOfString:currencySymbol withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [stringPasted length])]; - + [stringPasted replaceOccurrencesOfString:numberFormatter.groupingSeparator withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [stringPasted length])]; - + NSDecimalNumber *numberPasted = [NSDecimalNumber decimalNumberWithString:stringPasted]; stringMaybeChanged = [numberFormatter stringFromNumber:numberPasted]; } - + UITextRange *selectedRange = [textField selectedTextRange]; UITextPosition *start = textField.beginningOfDocument; NSInteger cursorOffset = [textField offsetFromPosition:start toPosition:selectedRange.start]; NSMutableString *textFieldTextStr = [NSMutableString stringWithString:textField.text]; NSUInteger textFieldTextStrLength = textFieldTextStr.length; - + [textFieldTextStr replaceCharactersInRange:range withString:stringMaybeChanged]; - - [textFieldTextStr replaceOccurrencesOfString:numberFormatter.currencySymbol - withString:@"" - options:NSLiteralSearch - range:NSMakeRange(0, [textFieldTextStr length])]; - + + [textFieldTextStr replaceOccurrencesOfString:currencySymbol + withString:@"" + options:NSLiteralSearch + range:NSMakeRange(0, [textFieldTextStr length])]; + [textFieldTextStr replaceOccurrencesOfString:numberFormatter.groupingSeparator withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [textFieldTextStr length])]; - + [textFieldTextStr replaceOccurrencesOfString:numberFormatter.decimalSeparator withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [textFieldTextStr length])]; - + if (textFieldTextStr.length <= MAX_DIGITS) { NSDecimalNumber *textFieldTextNum = [NSDecimalNumber decimalNumberWithString:textFieldTextStr]; NSDecimalNumber *divideByNum = [[[NSDecimalNumber alloc] initWithInt:10] decimalNumberByRaisingToPower:numberFormatter.maximumFractionDigits]; NSDecimalNumber *textFieldTextNewNum = [textFieldTextNum decimalNumberByDividingBy:divideByNum]; NSString *textFieldTextNewStr = [numberFormatter stringFromNumber:textFieldTextNewNum]; - + textField.text = textFieldTextNewStr; - + if (cursorOffset != textFieldTextStrLength) { NSInteger lengthDelta = textFieldTextNewStr.length - textFieldTextStrLength; @@ -137,7 +150,7 @@ - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange [textField setSelectedTextRange:newRange]; } } - + return NO; }