-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditableCurrencyCell.m
71 lines (59 loc) · 2.14 KB
/
EditableCurrencyCell.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
//
// EditableCurrencyCell.m
// Allowance
//
// Created by Pablo Collins on 6/19/10.
//
#import "EditableCurrencyCell.h"
@implementation EditableCurrencyCell
- (id)initWithPlaceholder:(NSString *)placeholder {
id out = [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
textField.placeholder = placeholder;
return out;
}
- (id)initwithValue:(NSString *)value {
id out = [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
textField.text = value;
return out;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
fieldInput = [[NSMutableString alloc] init];
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
CGRect frame = self.contentView.frame;
frame.origin.x = 20;
textField = [[UITextField alloc] initWithFrame:frame];
textField.delegate = self;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
textField.keyboardType = UIKeyboardTypeNumberPad;
[self addSubview:textField];
}
return self;
}
- (NSMutableString *)fieldInput {
if (fieldInput == nil) {
fieldInput = [NSMutableString stringWithFormat:@"%f",([textField.text doubleValue] * [AllowanceAppDelegate priceDenominator])];
}
return fieldInput;
}
- (double)doubleValue {
return [[self fieldInput] intValue] / [AllowanceAppDelegate priceDenominator];
}
- (NSString *)formattedInput {
return [NSString stringWithFormat:@"%.2f", [self doubleValue]];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//[super setSelected:selected animated:animated];
}
- (BOOL)textField:(UITextField *)tf shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([string length] == 0) { //delete btn pushed
range.location -= 1;
[[self fieldInput] deleteCharactersInRange:range];
} else {
[[self fieldInput]
appendString:string];
}
textField.text = [self formattedInput];
return NO;
}
@end