-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathDMPasscodeInternalViewController.m
More file actions
186 lines (159 loc) · 6.95 KB
/
Copy pathDMPasscodeInternalViewController.m
File metadata and controls
186 lines (159 loc) · 6.95 KB
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
//
// DMPasscodeInternalViewController.m
// Pods
//
// Created by Dylan Marriott on 20/09/14.
//
//
#import "DMPasscodeInternalViewController.h"
#import "DMPasscodeInternalField.h"
#import "DMPasscodeConfig.h"
#import <UIKit/UIKit.h>
#define IDIOM UI_USER_INTERFACE_IDIOM()
#define IPAD UIUserInterfaceIdiomPad
#define BELOW_FIELDS 190
#define ABOVE_FIELDS 65
@interface DMPasscodeInternalViewController () <UITextFieldDelegate>
@end
@implementation DMPasscodeInternalViewController {
__weak id<DMPasscodeInternalViewControllerDelegate> _delegate;
NSMutableArray* _textFields;
UITextField* _input;
UILabel* _instructions;
UILabel* _error;
DMPasscodeConfig* _config;
}
- (id)initWithDelegate:(id<DMPasscodeInternalViewControllerDelegate>)delegate config:(DMPasscodeConfig *)config {
if (self = [super init]) {
_delegate = delegate;
_config = config;
_instructions = [[UILabel alloc] init];
_error = [[UILabel alloc] init];
_textFields = [[NSMutableArray alloc] init];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = _config.backgroundColor;
self.navigationController.navigationBar.barTintColor = _config.navigationBarBackgroundColor;
UIBarButtonItem* closeItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(close:)];
closeItem.tintColor = _config.navigationBarForegroundColor;
self.navigationItem.leftBarButtonItem = closeItem;
self.navigationController.navigationBar.barStyle = (UIBarStyle)_config.statusBarStyle;
self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName :_config.navigationBarFont,
NSForegroundColorAttributeName: _config.navigationBarTitleColor};
self.title = _config.navigationBarTitle;
_instructions.frame = CGRectMake(0, 85, self.view.frame.size.width, 30);
_instructions.font = _config.instructionsFont;
_instructions.textColor = _config.descriptionColor;
_instructions.textAlignment = NSTextAlignmentCenter;
_instructions.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:_instructions];
_error.frame = CGRectMake(0, 190, 0, 0); // size set when text is set
_error.font = _config.errorFont;
_error.textColor = _config.errorForegroundColor;
_error.backgroundColor = _config.errorBackgroundColor;
_error.textAlignment = NSTextAlignmentCenter;
_error.layer.cornerRadius = 4;
_error.clipsToBounds = YES;
_error.alpha = 0;
_error.numberOfLines = 0;
_error.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[self.view addSubview:_error];
CGFloat y_padding = 140;
CGFloat itemWidth = 24;
CGFloat space = 20;
CGFloat totalWidth = (itemWidth * 4) + (space * 3);
CGFloat x_padding = (self.view.bounds.size.width - totalWidth) / 2;
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(x_padding, y_padding, totalWidth, itemWidth)];
container.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
for (int i = 0; i < 4; i++) {
DMPasscodeInternalField* field = [[DMPasscodeInternalField alloc] initWithFrame:CGRectMake(((itemWidth + space) * i), 0, itemWidth, itemWidth) config:_config];
UITapGestureRecognizer* singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[field addGestureRecognizer:singleFingerTap];
[container addSubview:field];
[_textFields addObject:field];
}
[self.view addSubview:container];
_input = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[_input setDelegate:self];
[_input addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
_input.keyboardType = UIKeyboardTypeNumberPad;
_input.keyboardAppearance = _config.inputKeyboardAppearance;
[self.view addSubview:_input];
[_input becomeFirstResponder];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
{
[self setErrorMessage:_error.text];
if(UIInterfaceOrientationIsPortrait(orientation))
{
_instructions.alpha = 1.0f;
}
}
-(void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
if([_input isFirstResponder]){
[_input resignFirstResponder];
}
[_input becomeFirstResponder];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger oldLength = [textField.text length];
NSUInteger replacementLength = [string length];
NSUInteger rangeLength = range.length;
NSUInteger newLength = oldLength - rangeLength + replacementLength;
BOOL returnKey = [string rangeOfString: @"\n"].location != NSNotFound;
return newLength <= 4|| returnKey;
}
- (void)editingChanged:(UITextField *)sender {
for (int i = 0; i < sender.text.length; i++) {
DMPasscodeInternalField* field = [_textFields objectAtIndex:i];
NSRange range;
range.length = 1;
range.location = i;
field.text = [sender.text substringWithRange:range];
}
for (int i = (int)sender.text.length; i < 4; i++) {
DMPasscodeInternalField* field = [_textFields objectAtIndex:i];
field.text = @"";
}
NSString* code = sender.text;
if (code.length == 4) {
[_delegate enteredCode:code];
}
}
- (void)close:(id)sender {
[_input resignFirstResponder];
[self dismissViewControllerAnimated:YES completion:nil];
[_delegate canceled];
}
- (void)reset {
for (DMPasscodeInternalField* field in _textFields) {
field.text = @"";
}
_input.text = @"";
}
- (void)setErrorMessage:(NSString *)errorMessage {
_error.text = errorMessage;
_error.alpha = errorMessage.length > 0 ? 1.0f : 0.0f;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if( IDIOM != IPAD && UIInterfaceOrientationIsLandscape(orientation))
{
_instructions.alpha = errorMessage.length > 0 ? 0.0f : 1.0f;
}
CGSize size = [_error.text sizeWithAttributes:@{NSFontAttributeName: _error.font}];
size.width += 28;
size.height += 28;
_error.frame = CGRectMake(self.view.frame.size.width / 2 - size.width / 2, _error.frame.origin.y, size.width, size.height);
if((orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) && IDIOM != IPAD ){
_error.frame = CGRectMake(self.view.frame.size.width / 2 - size.width / 2, ABOVE_FIELDS, size.width, size.height);
}
else {
_error.frame = CGRectMake(self.view.frame.size.width / 2 - size.width / 2, BELOW_FIELDS, size.width, size.height);
}
}
- (void)setInstructions:(NSString *)instructions {
_instructions.text = instructions;
}
@end