forked from Hackmodford/HMFCurrencyFormatter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MSCurrencyFormatter.m
335 lines (281 loc) · 11.2 KB
/
MSCurrencyFormatter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//
// MSCurrencyFormatter.m
// MA Mobile
//
// Created by Brandon Butler on 8/30/12.
// Copyright (c) 2012 POS Management. All rights reserved.
//
#import "MSCurrencyFormatter.h"
@interface MSCurrencyFormatter ()
@property (nonatomic, strong) NSLocale *locale;
@property (nonatomic, strong) NSNumberFormatter *formatter;
@property (nonatomic, strong) UIButton *toggleButton;
@property (nonatomic, strong) UIButton *doubleZerosButton;
@property (nonatomic, weak) UITextField *assignedTextField;
@property (nonatomic, assign) BOOL withToggleButton;
@property (nonatomic, assign) BOOL withDoubleZerosButton;
@property (nonatomic, assign) BOOL newButton;
@property (nonatomic, assign) BOOL keyboardDidShow;
@end
@implementation MSCurrencyFormatter
- (id)initWithLocale:(NSLocale *)locale withExtraButton:(int)extraButton
{
if (self = [super init]) {
[self setLocale:locale];
if (extraButton == 1) {
[self setWithToggleButton:YES];
} else if (extraButton == 2) {
[self setWithDoubleZerosButton:YES];
} else {
[self setWithToggleButton:NO];
[self setWithDoubleZerosButton:NO];
}
[self setFormatter:[[NSNumberFormatter alloc] init]];
[[self formatter] setNumberStyle:NSNumberFormatterCurrencyStyle];
[[self formatter] setLocale:[self locale]];
if ([self withToggleButton] && ![self toggleButton]) {
[self setToggleButton:[UIButton buttonWithType:UIButtonTypeCustom]];
[[self toggleButton] setFrame:CGRectMake(0, 163, 105, 53)];
[[self toggleButton] setAdjustsImageWhenHighlighted:NO];
[[self toggleButton] setImage:[UIImage imageNamed:@"toggleButtonUp.png"] forState:UIControlStateNormal];
[[self toggleButton] setImage:[UIImage imageNamed:@"toggleButtonDown.png"] forState:UIControlStateHighlighted];
[[self toggleButton] addTarget:self action:@selector(toggleButton:) forControlEvents:UIControlEventTouchUpInside];
} else if ([self withDoubleZerosButton] && ![self doubleZerosButton]) {
[self setDoubleZerosButton:[UIButton buttonWithType:UIButtonTypeCustom]];
[[self doubleZerosButton] setFrame:CGRectMake(0, 163, 105, 53)];
[[self doubleZerosButton] setAdjustsImageWhenHighlighted:NO];
[[self doubleZerosButton] setImage:[UIImage imageNamed:@"doubleZerosButtonUp.png"] forState:UIControlStateNormal];
[[self doubleZerosButton] setImage:[UIImage imageNamed:@"doubleZerosButtonDown.png"] forState:UIControlStateHighlighted];
[[self doubleZerosButton] addTarget:self action:@selector(doubleZerosButton:) forControlEvents:UIControlEventTouchUpInside];
}
[self setNewButton:TRUE];
}
return self;
}
- (id)initWithLocale:(NSLocale *)locale withDoubleZerosButton:(BOOL)doubleZerosButton
{
int toggle = 0;
if (doubleZerosButton) toggle = 2;
return [self initWithLocale:locale withExtraButton:toggle];
}
- (id)initWithLocale:(NSLocale *)locale withToggleButton:(BOOL)toggleButton
{
int toggle = 0;
if (toggleButton) toggle = 1;
return [self initWithLocale:locale withExtraButton:toggle];
}
- (id)initWithLocale:(NSLocale *)locale
{
return [self initWithLocale:locale withToggleButton:NO];
}
- (id)init
{
return [self initWithLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
}
#pragma mark UIKeyboard Notifications
- (void)dealloc
{
[self endWatchingForKeyboard];
}
- (void)startWatchingForKeyboardFromTextField:(UITextField *)textField
{
self.assignedTextField = textField;
self.assignedTextField.keyboardType = UIKeyboardTypeNumberPad;
if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"]) {
if ([self withToggleButton] || [self withDoubleZerosButton]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardStartedEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasDismissed:) name:UIKeyboardWillHideNotification object:nil];
}
}
- (void)keyboardDidShow:(NSNotification *)note
{
[self setKeyboardDidShow:TRUE];
if ([[self assignedTextField] isFirstResponder] && [self newButton]) [self addButtonToKeyboard];
}
- (void)keyboardStartedEditing:(NSNotification *)note
{
if ([[self assignedTextField] isFirstResponder]) {
if (([self withToggleButton] || [self withDoubleZerosButton]) && [self newButton] && [self keyboardDidShow]) [self addButtonToKeyboard];
if ([[[self assignedTextField] text] length] == 0 && [[[self assignedTextField] delegate] isEqual:self]) {
[[self assignedTextField] setText:[[self formatter] stringFromNumber:[NSNumber numberWithFloat:0.0]]];
}
if ([self withToggleButton]) {
[[self toggleButton] setHidden:NO];
[[self toggleButton] setUserInteractionEnabled:YES];
} else if ([self withDoubleZerosButton]) {
[[self doubleZerosButton] setHidden:NO];
[[self doubleZerosButton] setUserInteractionEnabled:YES];
}
} else {
if ([self withToggleButton]) {
[[self toggleButton] setHidden:YES];
[[self toggleButton] setUserInteractionEnabled:NO];
} else if ([self withDoubleZerosButton]) {
[[self doubleZerosButton] setHidden:YES];
[[self doubleZerosButton] setUserInteractionEnabled:NO];
}
}
}
- (void)keyboardWasDismissed:(NSNotification *)note
{
if ([self doesAlertViewExist]) [self endWatchingForKeyboard];
[self setNewButton:TRUE];
if ([self withToggleButton] || [self withDoubleZerosButton]) [self setKeyboardDidShow:FALSE];
}
- (void)endWatchingForKeyboard
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark Private Methods
- (void)addButtonToKeyboard
{
NSInteger viewIndex = ([self doesAlertViewExist]) ? 2 : 1;
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:viewIndex];
for (UIView *keyboard in [tempWindow subviews]) {
if ([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) {
if ([self withToggleButton]) {
[keyboard addSubview:[self toggleButton]];
} else if ([self withDoubleZerosButton]) {
[keyboard addSubview:[self doubleZerosButton]];
}
[self setNewButton:FALSE];
}
}
}
- (void)toggleButton:(id)sender
{
if ([[[self assignedTextField] text] length] > 0) {
[[[self assignedTextField] delegate] textField:[self assignedTextField] shouldChangeCharactersInRange:NSMakeRange(0, 1) replacementString:@"-"];
}
}
- (void)doubleZerosButton:(id)sender
{
if ([[[self assignedTextField] text] length] > 0) {
for (int i = 0; i < 2; i++) {
[[[self assignedTextField] delegate] textField:[self assignedTextField] shouldChangeCharactersInRange:NSMakeRange(-1, 1) replacementString:@"0"];
}
}
}
- (BOOL)doesAlertViewExist
{
for (UIWindow* window in [[UIApplication sharedApplication] windows]) {
for (UIView* view in [window subviews]) {
if ([view isKindOfClass:[UIAlertView class]] || [view isKindOfClass:[UIActionSheet class]]) return YES;
}
}
return NO;
}
#pragma mark UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
[textField setText:[self formatTextField:textField withCharactersInRange:range withReplacementString:string]];
return NO;
}
- (NSString *)formatTextField:(UITextField *)textField withCharactersInRange:(NSRange)range withReplacementString:(NSString *)string
{
NSString *minus = @"-";
if ([string hasSuffix:minus]) {
if ([[[textField text] substringToIndex:1] isEqualToString:minus]) {
return textField.text = [[textField text] substringFromIndex:1];
}
return textField.text = [minus stringByAppendingString:[textField text]];
}
if (range.location == 0 && range.length == 1 && [string isEqualToString:@""]) {
return [[textField text] substringFromIndex:1];
}
NSInteger currencyScale = -1 * [[self formatter] maximumFractionDigits];
NSMutableString *filteredString = [NSMutableString stringWithCapacity:[[textField text] length]];
NSScanner *scanner = [NSScanner scannerWithString:[textField text]];
NSCharacterSet *allowedChars = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:allowedChars intoString:&buffer]) {
[filteredString appendString:buffer];
} else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
NSString *enteredDigits = filteredString;
BOOL isNegative = [[[textField text] substringToIndex:1] isEqualToString:minus];
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
for (int i = 0; i < [string length]; i++) {
if (![myCharSet characterIsMember:[string characterAtIndex:i]]) return [textField text];
}
if ([string length]) {
if ([enteredDigits length] > 10) {
return [textField text];
} else {
enteredDigits = [enteredDigits stringByAppendingFormat:@"%d", [string integerValue]];
}
} else {
NSUInteger len = [enteredDigits length];
if (len > 1) {
enteredDigits = [enteredDigits substringWithRange:NSMakeRange(0, len - 1)];
} else {
enteredDigits = @"";
}
}
NSDecimalNumber *decimal = nil;
if (![enteredDigits isEqualToString:@""]) {
decimal = [[NSDecimalNumber decimalNumberWithString:enteredDigits] decimalNumberByMultiplyingByPowerOf10:currencyScale];
} else {
decimal = [NSDecimalNumber zero];
}
NSString *results = @"";
if (isNegative) {
results = [NSString stringWithFormat:@"-%@",[[self formatter] stringFromNumber:decimal]];
} else {
results = [[self formatter] stringFromNumber:decimal];
}
return results;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ((void (^)(void))self.textFieldShouldBeginEditingBlock) {
self.textFieldShouldBeginEditingBlock(textField);
}
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ((void (^)(void))self.textFieldDidBeginEditingBlock) {
self.textFieldDidBeginEditingBlock(textField);
}
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if ((void (^)(void))self.textFieldShouldEndEditingBlock) {
self.textFieldShouldEndEditingBlock(textField);
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ((void (^)(void))self.textFieldDidEndEditingBlock) {
self.textFieldDidEndEditingBlock(textField);
}
}
#pragma mark Class Methods
+ (NSDecimalNumber *)decimalNumberFromFormattedString:(NSString *)string
{
NSMutableString *strippedString = [NSMutableString stringWithCapacity:[string length]];
NSScanner *scanner = [NSScanner scannerWithString:string];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789.-"];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
[strippedString appendString:buffer];
} else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
return [NSDecimalNumber decimalNumberWithString:strippedString];
}
@end