Skip to content

Commit e2a1f51

Browse files
committed
Merge branch 'master' of github.com:venmo/VENCalculatorInputView
2 parents e4eca00 + e2ab504 commit e2a1f51

7 files changed

Lines changed: 84 additions & 4 deletions

File tree

Podfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ platform :ios, '7.0'
33
target 'VENCalculatorInputViewTests', :exclusive => true do
44
pod 'Specta', '~> 0.2.1'
55
pod 'Expecta', '~> 0.2.2'
6+
pod 'OCMock', '~>3.0.0'
67
end

Podfile.lock

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
PODS:
2-
- Expecta (0.2.3)
2+
- Expecta (0.2.4)
3+
- OCMock (3.0.2)
34
- Specta (0.2.1)
45

56
DEPENDENCIES:
67
- Expecta (~> 0.2.2)
8+
- OCMock (~> 3.0.0)
79
- Specta (~> 0.2.1)
810

911
SPEC CHECKSUMS:
10-
Expecta: 578e0c29df79a96a159187599e2def686ef6a66c
12+
Expecta: 112bcafa2304ee0f3c5e586505f24555a47b25d5
13+
OCMock: b9836ab89d8d5e66cbe6333f93857037c310ee62
1114
Specta: 9141310f46b1f68b676650ff2854e1ed0b74163a
1215

1316
COCOAPODS: 0.33.1

VENCalculatorInputView.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
2F74D09D196F533B0040C816 /* UITextField+VENCalculatorInputView.m in Sources */ = {isa = PBXBuildFile; fileRef = 2F74D09C196F533B0040C816 /* UITextField+VENCalculatorInputView.m */; };
11+
2F74D09F196F53E20040C816 /* UITextField+VENCalculatorInputViewSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 2F74D09E196F53E20040C816 /* UITextField+VENCalculatorInputViewSpec.m */; };
1012
40BED60EE864401D8AFB2433 /* libPods-VENCalculatorInputViewTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 142758DF9FEC443BA2D635DE /* libPods-VENCalculatorInputViewTests.a */; };
1113
B6F22CFD18AC031100543816 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B6F22CFC18AC031100543816 /* Foundation.framework */; };
1214
B6F22D0C18AC031100543816 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B6F22CFC18AC031100543816 /* Foundation.framework */; };
@@ -47,6 +49,9 @@
4749

4850
/* Begin PBXFileReference section */
4951
142758DF9FEC443BA2D635DE /* libPods-VENCalculatorInputViewTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-VENCalculatorInputViewTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
52+
2F74D09B196F533B0040C816 /* UITextField+VENCalculatorInputView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITextField+VENCalculatorInputView.h"; sourceTree = "<group>"; };
53+
2F74D09C196F533B0040C816 /* UITextField+VENCalculatorInputView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITextField+VENCalculatorInputView.m"; sourceTree = "<group>"; };
54+
2F74D09E196F53E20040C816 /* UITextField+VENCalculatorInputViewSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITextField+VENCalculatorInputViewSpec.m"; sourceTree = "<group>"; };
5055
8ECCE86895A54E0F8FA38DA9 /* Pods-VENCalculatorInputViewTests.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-VENCalculatorInputViewTests.xcconfig"; path = "Pods/Pods-VENCalculatorInputViewTests.xcconfig"; sourceTree = "<group>"; };
5156
B6F22CF918AC031100543816 /* libVENCalculatorInputView.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libVENCalculatorInputView.a; sourceTree = BUILT_PRODUCTS_DIR; };
5257
B6F22CFC18AC031100543816 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -135,6 +140,8 @@
135140
B6F22D2B18AC039F00543816 /* VENCalculatorInputView.h */,
136141
B6F22D2C18AC039F00543816 /* VENCalculatorInputView.m */,
137142
B6F22D2D18AC039F00543816 /* VENCalculatorInputView.xib */,
143+
2F74D09B196F533B0040C816 /* UITextField+VENCalculatorInputView.h */,
144+
2F74D09C196F533B0040C816 /* UITextField+VENCalculatorInputView.m */,
138145
B6FD6360196BAB8700D1913C /* NSString+VENCalculatorInputView.h */,
139146
B6FD6361196BAB8700D1913C /* NSString+VENCalculatorInputView.m */,
140147
B6F22D3118AC03C700543816 /* Images */,
@@ -154,6 +161,7 @@
154161
B6F22D1218AC031100543816 /* VENCalculatorInputViewTests */ = {
155162
isa = PBXGroup;
156163
children = (
164+
2F74D09E196F53E20040C816 /* UITextField+VENCalculatorInputViewSpec.m */,
157165
B6F22D2218AC039100543816 /* VENMoneyCalculatorSpec.m */,
158166
B6FD6363196BB09D00D1913C /* NSString+VENCalculatorInputViewSpec.m */,
159167
B6F22D1318AC031100543816 /* Supporting Files */,
@@ -301,13 +309,15 @@
301309
B6F22D3018AC039F00543816 /* VENCalculatorInputView.m in Sources */,
302310
B6F22D2E18AC039F00543816 /* VENMoneyCalculator.m in Sources */,
303311
B6F22D2F18AC039F00543816 /* VENCalculatorInputTextField.m in Sources */,
312+
2F74D09D196F533B0040C816 /* UITextField+VENCalculatorInputView.m in Sources */,
304313
);
305314
runOnlyForDeploymentPostprocessing = 0;
306315
};
307316
B6F22D0518AC031100543816 /* Sources */ = {
308317
isa = PBXSourcesBuildPhase;
309318
buildActionMask = 2147483647;
310319
files = (
320+
2F74D09F196F53E20040C816 /* UITextField+VENCalculatorInputViewSpec.m in Sources */,
311321
B6F22D2318AC039100543816 /* VENMoneyCalculatorSpec.m in Sources */,
312322
B6FD6364196BB09D00D1913C /* NSString+VENCalculatorInputViewSpec.m in Sources */,
313323
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#import <UIKit/UIKit.h>
2+
3+
@interface UITextField (VENCalculatorInputView)
4+
5+
- (NSRange)selectedNSRange;
6+
7+
@end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#import "UITextField+VENCalculatorInputView.h"
2+
3+
@implementation UITextField (VENCalculatorInputView)
4+
5+
- (NSRange)selectedNSRange
6+
{
7+
UITextPosition *beginning = self.beginningOfDocument;
8+
9+
UITextRange *selectedRange = self.selectedTextRange;
10+
UITextPosition *selectionStart = selectedRange.start;
11+
UITextPosition *selectionEnd = selectedRange.end;
12+
13+
NSInteger location = [self offsetFromPosition:beginning toPosition:selectionStart];
14+
NSInteger length = [self offsetFromPosition:selectionStart toPosition:selectionEnd];
15+
16+
return NSMakeRange(location, length);
17+
}
18+
19+
@end

VENCalculatorInputView/VENCalculatorInputTextField.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import "VENCalculatorInputTextField.h"
22
#import "VENMoneyCalculator.h"
3+
#import "UITextField+VENCalculatorInputView.h"
34

45
@interface VENCalculatorInputTextField ()
56
@property (strong, nonatomic) VENMoneyCalculator *moneyCalculator;
@@ -40,7 +41,7 @@ - (void)setUpInit {
4041

4142
- (void)setLocale:(NSLocale *)locale {
4243
_locale = locale;
43-
VENCalculatorInputView *inputView = self.inputView;
44+
VENCalculatorInputView *inputView = (VENCalculatorInputView *)self.inputView;
4445
inputView.locale = locale;
4546
self.moneyCalculator.locale = locale;
4647
}
@@ -83,7 +84,14 @@ - (void)venCalculatorTextFieldDidEndEditing {
8384
#pragma mark - VENCalculatorInputViewDelegate
8485

8586
- (void)calculatorInputView:(VENCalculatorInputView *)inputView didTapKey:(NSString *)key {
86-
[self insertText:key];
87+
if ([self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
88+
NSRange range = [self selectedNSRange];
89+
if ([self.delegate textField:self shouldChangeCharactersInRange:range replacementString:key]) {
90+
[self insertText:key];
91+
}
92+
} else {
93+
[self insertText:key];
94+
}
8795
}
8896

8997
- (void)calculatorInputViewDidTapBackspace:(VENCalculatorInputView *)calculatorInputView {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#define EXP_SHORTHAND
2+
#import <Expecta/Expecta.h>
3+
#import <Specta/Specta.h>
4+
#import <OCMock/OCMock.h>
5+
6+
#import "UITextField+VENCalculatorInputView.h"
7+
8+
SpecBegin(UITextRange_VENCalculatorInputView)
9+
10+
describe(@"selectedNSRange", ^{
11+
12+
it(@"should get UITextField's selectedText in an NSRange", ^{
13+
id mockBeginningTextPosition = OCMClassMock([UITextPosition class]);
14+
id mockSelectionStartTextPosition = OCMClassMock([UITextPosition class]);
15+
id mockSelectionEndTextPosition = OCMClassMock([UITextPosition class]);
16+
17+
id mockTextField = OCMClassMock([UITextField class]);
18+
id mockSelectedTextRange = OCMClassMock([UITextRange class]);
19+
20+
OCMStub([mockTextField beginningOfDocument]).andReturn(mockBeginningTextPosition);
21+
OCMStub([mockTextField selectedTextRange]).andReturn(mockSelectedTextRange);
22+
OCMStub([(UITextRange *)mockSelectedTextRange start]).andReturn(mockSelectionStartTextPosition);
23+
OCMStub([(UITextRange *)mockSelectedTextRange end]).andReturn(mockSelectionEndTextPosition);
24+
25+
[[mockTextField expect] offsetFromPosition:mockBeginningTextPosition toPosition:mockSelectionStartTextPosition];
26+
[[mockTextField expect] offsetFromPosition:mockSelectionStartTextPosition toPosition:mockSelectionEndTextPosition];
27+
28+
[mockTextField selectedNSRange];
29+
});
30+
});
31+
32+
SpecEnd

0 commit comments

Comments
 (0)