Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathBKPasscodeInputView.m
More file actions
492 lines (392 loc) · 13.5 KB
/
Copy pathBKPasscodeInputView.m
File metadata and controls
492 lines (392 loc) · 13.5 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//
// BKPasscodeInputView.m
// BKPasscodeViewDemo
//
// Created by Byungkook Jang on 2014. 4. 20..
// Copyright (c) 2014년 Byungkook Jang. All rights reserved.
//
#import "BKPasscodeInputView.h"
#define kLabelPasscodeSpacePortrait (30.0f)
#define kLabelPasscodeSpaceLandscape (10.0f)
#define kTextLeftRightSpace (20.0f)
#define kErrorMessageLeftRightPadding (10.0f)
#define kErrorMessageTopBottomPadding (5.0f)
#define kDefaultNumericPasscodeMaximumLength (4)
#define kDefaultNormalPasscodeMaximumLength (20)
@interface BKPasscodeInputView () {
BOOL _isKeyboardTypeSet;
}
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UILabel *errorMessageLabel;
@property (nonatomic, strong) UIControl *passcodeField;
@end
@implementation BKPasscodeInputView
@synthesize maximumLength = _maximumLength;
@synthesize keyboardType = _keyboardType;
@synthesize passcodeField = _passcodeField;
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self _initialize];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self _initialize];
}
return self;
}
- (void)_initialize
{
self.backgroundColor = [UIColor clearColor];
_enabled = YES;
_passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
_keyboardType = UIKeyboardTypeNumberPad;
_maximumLength = 0;
_titleLabel = [[UILabel alloc] init];
[[self class] configureTitleLabel:_titleLabel];
[self addSubview:_titleLabel];
_messageLabel = [[UILabel alloc] init];
[[self class] configureMessageLabel:_messageLabel];
[self addSubview:_messageLabel];
_errorMessageLabel = [[UILabel alloc] init];
[[self class] configureErrorMessageLabel:_errorMessageLabel];
_errorMessageLabel.hidden = YES;
[self addSubview:_errorMessageLabel];
}
+ (void)configureTitleLabel:(UILabel *)aLabel
{
aLabel.backgroundColor = [UIColor clearColor];
aLabel.numberOfLines = 1;
aLabel.textAlignment = NSTextAlignmentCenter;
aLabel.lineBreakMode = NSLineBreakByTruncatingTail;
aLabel.font = [UIFont boldSystemFontOfSize:15.0f];
}
+ (void)configureMessageLabel:(UILabel *)aLabel
{
aLabel.backgroundColor = [UIColor clearColor];
aLabel.numberOfLines = 0;
aLabel.textAlignment = NSTextAlignmentCenter;
aLabel.lineBreakMode = NSLineBreakByWordWrapping;
aLabel.font = [UIFont systemFontOfSize:15.0f];
}
+ (void)configureErrorMessageLabel:(UILabel *)aLabel
{
aLabel.backgroundColor = [UIColor clearColor];
aLabel.numberOfLines = 0;
aLabel.textAlignment = NSTextAlignmentCenter;
aLabel.lineBreakMode = NSLineBreakByWordWrapping;
aLabel.backgroundColor = [UIColor colorWithRed:0.63 green:0.2 blue:0.13 alpha:1];
aLabel.textColor = [UIColor whiteColor];
aLabel.font = [UIFont systemFontOfSize:15.0f];
aLabel.layer.cornerRadius = 10.0f;
aLabel.layer.masksToBounds = YES;
}
- (void)setPasscodeStyle:(BKPasscodeInputViewPasscodeStyle)passcodeStyle
{
if (_passcodeStyle != passcodeStyle) {
_passcodeStyle = passcodeStyle;
if (_passcodeField) {
_passcodeField = nil;
[self passcodeField]; // load passcode field immediately if already exists before.
}
}
}
- (UIControl *)passcodeField
{
if (nil == _passcodeField) {
switch (_passcodeStyle) {
case BKPasscodeInputViewNumericPasscodeStyle:
{
if (_maximumLength == 0) {
_maximumLength = kDefaultNumericPasscodeMaximumLength;
}
if (NO == _isKeyboardTypeSet) {
_keyboardType = UIKeyboardTypeNumberPad;
}
BKPasscodeField *passcodeField = [[BKPasscodeField alloc] init];
passcodeField.delegate = self;
passcodeField.keyboardType = _keyboardType;
passcodeField.maximumLength = _maximumLength;
[passcodeField addTarget:self action:@selector(passcodeControlEditingChanged:) forControlEvents:UIControlEventEditingChanged];
[self setPasscodeField:passcodeField];
break;
}
case BKPasscodeInputViewNormalPasscodeStyle:
{
if (_maximumLength == 0) {
_maximumLength = kDefaultNormalPasscodeMaximumLength;
}
if (NO == _isKeyboardTypeSet) {
_keyboardType = UIKeyboardTypeASCIICapable;
}
UITextField *textField = [[UITextField alloc] init];
textField.delegate = self;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.spellCheckingType = UITextSpellCheckingTypeNo;
textField.enablesReturnKeyAutomatically = YES;
textField.keyboardType = _keyboardType;
textField.secureTextEntry = YES;
textField.font = [UIFont systemFontOfSize:25.0f];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.returnKeyType = UIReturnKeyDone;
[self setPasscodeField:textField];
break;
}
}
}
return _passcodeField;
}
- (void)setPasscodeField:(UIControl *)passcodeField
{
if (_passcodeField != passcodeField) {
[_passcodeField removeFromSuperview];
_passcodeField = passcodeField;
if (_passcodeField) {
[self addSubview:_passcodeField];
}
[self setNeedsLayout];
}
}
- (void)setMaximumLength:(NSUInteger)maximumLength
{
_maximumLength = maximumLength;
if ([_passcodeField isKindOfClass:[BKPasscodeField class]]) {
[(BKPasscodeField *)_passcodeField setMaximumLength:maximumLength];
}
}
- (void)setKeyboardType:(UIKeyboardType)keyboardType
{
_isKeyboardTypeSet = YES;
_keyboardType = keyboardType;
[(id<UITextInputTraits>)_passcodeField setKeyboardType:keyboardType];
}
- (void)setTitle:(NSString *)title
{
self.titleLabel.text = title;
[self setNeedsLayout];
}
- (NSString *)title
{
return self.titleLabel.text;
}
- (void)setMessage:(NSString *)message
{
self.messageLabel.text = message;
self.messageLabel.hidden = NO;
self.errorMessageLabel.text = nil;
self.errorMessageLabel.hidden = YES;
[self setNeedsLayout];
}
- (NSString *)message
{
return self.messageLabel.text;
}
- (void)setErrorMessage:(NSString *)errorMessage
{
self.errorMessageLabel.text = errorMessage;
self.errorMessageLabel.hidden = NO;
self.messageLabel.text = nil;
self.messageLabel.hidden = YES;
[self setNeedsLayout];
}
- (NSString *)errorMessage
{
return self.errorMessageLabel.text;
}
- (NSString *)passcode
{
switch (self.passcodeStyle) {
case BKPasscodeInputViewNumericPasscodeStyle:
return [(BKPasscodeField *)self.passcodeField passcode];
case BKPasscodeInputViewNormalPasscodeStyle:
return [(UITextField *)self.passcodeField text];
}
}
- (void)setPasscode:(NSString *)passcode
{
switch (self.passcodeStyle) {
case BKPasscodeInputViewNumericPasscodeStyle:
[(BKPasscodeField *)self.passcodeField setPasscode:passcode];
break;
case BKPasscodeInputViewNormalPasscodeStyle:
[(UITextField *)self.passcodeField setText:passcode];
break;
}
}
#pragma mark - Customizations
- (UIFont *)titleFont
{
return self.titleLabel.font;
}
- (void)setTitleFont:(UIFont *)font
{
self.titleLabel.font = font;
}
- (UIFont *)messageFont
{
return self.messageLabel.font;
}
- (void)setMessageFont:(UIFont *)font
{
self.messageLabel.font = font;
}
- (UIFont *)errorMessageFont
{
return self.errorMessageLabel.font;
}
- (void)setErrorMessageFont:(UIFont *)font
{
self.errorMessageLabel.font = font;
}
- (UIColor *)titleColor
{
return self.titleLabel.textColor;
}
- (void)setTitleColor:(UIColor *)color
{
self.titleLabel.textColor = color;
}
- (UIColor *)messageColor
{
return self.messageLabel.textColor;
}
- (void)setMessageColor:(UIColor *)color
{
self.messageLabel.textColor = color;
}
- (UIColor *)errorMessageColor
{
return self.errorMessageLabel.textColor;
}
- (void)setErrorMessageColor:(UIColor *)color
{
self.errorMessageLabel.textColor = color;
}
#pragma mark - UIView
- (CGFloat)labelPasscodeSpace
{
return UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? kLabelPasscodeSpacePortrait : kLabelPasscodeSpaceLandscape;
}
- (void)layoutSubviews
{
[super layoutSubviews];
// layout passcode control to center
[self.passcodeField sizeToFit];
if ([self.passcodeField isKindOfClass:[UITextField class]]) {
self.passcodeField.frame = CGRectMake(0, 0, self.frame.size.width - kTextLeftRightSpace * 2.0f, CGRectGetHeight(self.passcodeField.frame) + 10.0f);
}
self.passcodeField.center = CGPointMake(CGRectGetWidth(self.frame) * 0.5f, CGRectGetHeight(self.frame) * 0.5f);
CGFloat maxTextWidth = self.frame.size.width - (kTextLeftRightSpace * 2.0f);
CGFloat labelPasscodeSpace = [self labelPasscodeSpace];
// layout title label
_titleLabel.frame = CGRectMake(kTextLeftRightSpace, 0, maxTextWidth, self.frame.size.height);
[_titleLabel sizeToFit];
CGRect rect = _titleLabel.frame;
rect.origin.x = floorf((self.frame.size.width - CGRectGetWidth(rect)) * 0.5f);
rect.origin.y = CGRectGetMinY(self.passcodeField.frame) - labelPasscodeSpace - CGRectGetHeight(_titleLabel.frame);
_titleLabel.frame = rect;
// layout message label
if (!_messageLabel.hidden) {
_messageLabel.frame = CGRectMake(kTextLeftRightSpace, CGRectGetMaxY(self.passcodeField.frame) + labelPasscodeSpace, maxTextWidth, self.frame.size.height);
[_messageLabel sizeToFit];
rect = _messageLabel.frame;
rect.origin.x = floorf((self.frame.size.width - CGRectGetWidth(rect)) * 0.5f);
_messageLabel.frame = rect;
}
// layout error message label
if (!_errorMessageLabel.hidden) {
_errorMessageLabel.frame = CGRectMake(0, CGRectGetMaxY(self.passcodeField.frame) + labelPasscodeSpace,
maxTextWidth - kErrorMessageLeftRightPadding * 2.0f,
self.frame.size.height);
[_errorMessageLabel sizeToFit];
rect = _errorMessageLabel.frame;
rect.size.width += (kErrorMessageLeftRightPadding * 2.0f);
rect.size.height += (kErrorMessageTopBottomPadding * 2.0f);
rect.origin.x = floorf((self.frame.size.width - rect.size.width) * 0.5f);
_errorMessageLabel.frame = rect;
}
}
#pragma mark - UIResponder
- (BOOL)canBecomeFirstResponder
{
return [self.passcodeField canBecomeFirstResponder];
}
- (BOOL)becomeFirstResponder
{
return [self.passcodeField becomeFirstResponder];
}
- (BOOL)canResignFirstResponder
{
return [self.passcodeField canResignFirstResponder];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self.passcodeField becomeFirstResponder];
}
#pragma mark - Actions
- (void)passcodeControlEditingChanged:(id)sender
{
if (![self.passcodeField isKindOfClass:[BKPasscodeField class]]) {
return;
}
BKPasscodeField *passcodeField = (BKPasscodeField *)self.passcodeField;
if (passcodeField.passcode.length == passcodeField.maximumLength) {
if ([self.delegate respondsToSelector:@selector(passcodeInputViewDidFinish:)]) {
[self.delegate passcodeInputViewDidFinish:self];
}
}
}
#pragma mark - BKPasscodeFieldDelegate
- (BOOL)passcodeField:(BKPasscodeField *)aPasscodeField shouldInsertText:(NSString *)aText
{
return self.isEnabled;
}
- (BOOL)passcodeFieldShouldDeleteBackward:(BKPasscodeField *)aPasscodeField
{
return self.isEnabled;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (self.isEnabled == NO) {
return NO;
}
NSUInteger length = textField.text.length - range.length + string.length;
if (length > self.maximumLength) {
return NO;
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (self.isEnabled == NO) {
return NO;
}
if ([self.delegate respondsToSelector:@selector(passcodeInputViewDidFinish:)]) {
[self.delegate passcodeInputViewDidFinish:self];
return NO;
} else {
return YES; // default behavior
}
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone
{
BKPasscodeInputView *view = [[[self class] alloc] initWithFrame:self.bounds];
view.delegate = self.delegate;
view.autoresizingMask = self.autoresizingMask;
view.passcodeStyle = self.passcodeStyle;
view.keyboardType = self.keyboardType;
view.maximumLength = self.maximumLength;
return view;
}
@end
You can’t perform that action at this time.
