Skip to content

Commit 0cde450

Browse files
committed
Added warning label
1 parent 0806d61 commit 0cde450

File tree

7 files changed

+170
-4
lines changed

7 files changed

+170
-4
lines changed

TOPasscodeViewController/TOPasscodeSettingsViewController.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "TOPasscodeSettingsViewController.h"
1010
#import "TOPasscodeNumberInputView.h"
1111
#import "TOPasscodeSettingsKeypadView.h"
12+
#import "TOPasscodeSettingsWarningLabel.h"
1213

1314
const CGFloat kTOPasscodeSettingsLabelInputSpacing = 15.0f;
1415
const CGFloat kTOPasscodeKeypadMaxSizeRatio = 0.40f;
@@ -21,6 +22,7 @@ @interface TOPasscodeSettingsViewController ()
2122
@property (nonatomic, strong) UILabel *titleLabel;
2223
@property (nonatomic, strong) TOPasscodeNumberInputView *numberInputView;
2324
@property (nonatomic, strong) TOPasscodeSettingsKeypadView *keypadView;
25+
@property (nonatomic, strong) TOPasscodeSettingsWarningLabel *warningLabel;
2426

2527
@end
2628

@@ -70,13 +72,17 @@ - (void)viewDidLoad {
7072
self.keypadView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
7173
[self.view addSubview:self.keypadView];
7274

75+
// Create label view
76+
self.warningLabel = [[TOPasscodeSettingsWarningLabel alloc] initWithFrame:CGRectZero];
77+
[self.containerView addSubview:self.warningLabel];
78+
7379
// Add callbacks for the keypad view
7480
self.keypadView.numberButtonTappedHandler= ^(NSInteger number) {
7581
NSString *numberString = [NSString stringWithFormat:@"%ld", number];
7682
[weakSelf.numberInputView appendPasscodeCharacters:numberString animated:NO];
7783
};
7884

79-
self.keypadView.deleteButtonTappedHandler = ^{ [weakSelf.numberInputView deletePasscodeCharactersOfCount:1 animated:YES]; };
85+
self.keypadView.deleteButtonTappedHandler = ^{ [weakSelf.numberInputView deletePasscodeCharactersOfCount:1 animated:NO]; };
8086

8187
// Set height of the container view (This will never change)
8288
CGRect frame = self.containerView.frame;

TOPasscodeViewController/Views/TOPasscodeCircleView.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
5757
return;
5858
}
5959

60-
[UIView animateWithDuration:0.5f animations:animationBlock];
60+
[UIView animateWithDuration:0.45f animations:animationBlock];
6161
}
6262

6363
- (void)setCircleImage:(UIImage *)circleImage

TOPasscodeViewController/Views/TOPasscodeSettingsKeypadView.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
const CGFloat kTOPasscodeSettingsKeypadButtonInnerSpacing = 7.0f;
1515
const CGFloat kTOPasscodeSettingsKeypadButtonOuterSpacing = 7.0f;
16-
const CGFloat kTOPasscodeSettingsKeypadCornderRadius = 16.0f;
16+
const CGFloat kTOPasscodeSettingsKeypadCornderRadius = 10.0f;
1717

1818
@interface TOPasscodeSettingsKeypadView ()
1919

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// TOPasscodeSettingsWarningLabel.h
3+
// TOPasscodeViewControllerExample
4+
//
5+
// Created by Tim Oliver on 6/28/17.
6+
// Copyright © 2017 Timothy Oliver. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface TOPasscodeSettingsWarningLabel : UIImageView
12+
13+
/** The number of incorrect password attempts to display */
14+
@property (nonatomic, assign) NSInteger numberOfWarnings;
15+
16+
/** The font of the text */
17+
@property (nonatomic, strong) UIFont *textFont;
18+
19+
/** The background color of the view */
20+
@property (nonatomic, strong) UIColor *backgroundColor;
21+
22+
/** Set the padding around the label */
23+
@property (nonatomic, assign) CGSize textPadding;
24+
25+
@end
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//
2+
// TOPasscodeSettingsWarningLabel.m
3+
// TOPasscodeViewControllerExample
4+
//
5+
// Created by Tim Oliver on 6/28/17.
6+
// Copyright © 2017 Timothy Oliver. All rights reserved.
7+
//
8+
9+
#import "TOPasscodeSettingsWarningLabel.h"
10+
11+
@interface TOPasscodeSettingsWarningLabel ()
12+
@property (nonatomic, strong) UILabel *label;
13+
@end
14+
15+
@implementation TOPasscodeSettingsWarningLabel
16+
17+
@synthesize backgroundColor = __backgroundColor;
18+
19+
#pragma mark - View Setup -
20+
21+
- (instancetype)initWithFrame:(CGRect)frame
22+
{
23+
if (self = [super initWithFrame:frame]) {
24+
[self setUp];
25+
}
26+
27+
return self;
28+
}
29+
30+
- (void)setUp
31+
{
32+
_textPadding = CGSizeMake(12.0f, 4.0f);
33+
34+
self.tintColor = [UIColor colorWithRed:214.0f/255.0f green:63.0f/255.0f blue:63.0f/255.0f alpha:1.0f];
35+
36+
self.label = [[UILabel alloc] initWithFrame:CGRectZero];
37+
self.label.textAlignment = NSTextAlignmentCenter;
38+
self.label.textColor = [UIColor whiteColor];
39+
self.label.font = [UIFont systemFontOfSize:15.0f];
40+
[self setTextForCount:0];
41+
[self.label sizeToFit];
42+
[self addSubview:self.label];
43+
}
44+
45+
- (void)didMoveToSuperview
46+
{
47+
[super didMoveToSuperview];
48+
[self setBackgroundImageIfNeeded];
49+
}
50+
51+
#pragma mark - View Layout -
52+
53+
- (void)sizeToFit
54+
{
55+
[super sizeToFit];
56+
CGRect frame = self.frame;
57+
CGRect labelFrame = self.label.frame;
58+
59+
labelFrame = CGRectInset(labelFrame, -self.textPadding.width, -self.textPadding.height);
60+
frame.size = labelFrame.size;
61+
frame.origin.x = CGRectGetMidX(frame) - (CGRectGetWidth(labelFrame) * 0.5f);
62+
frame.origin.y = CGRectGetMidY(frame) - (CGRectGetHeight(labelFrame) * 0.5f);
63+
self.frame = frame;
64+
}
65+
66+
- (void)layoutSubviews
67+
{
68+
CGRect frame = self.frame;
69+
CGRect labelFrame = self.label.frame;
70+
71+
labelFrame.origin.x = (CGRectGetWidth(frame) - CGRectGetWidth(labelFrame)) * 0.5f;
72+
labelFrame.origin.y = (CGRectGetWidth(frame) - CGRectGetWidth(labelFrame)) * 0.5f;
73+
self.label.frame = labelFrame;
74+
}
75+
76+
#pragma mark - View State Handling -
77+
78+
- (void)setTextForCount:(NSInteger)count
79+
{
80+
NSString *text = nil;
81+
if (count == 1) {
82+
text = NSLocalizedString(@"1 Failed Passcode Attempt", @"");
83+
}
84+
else {
85+
text = [NSString stringWithFormat:NSLocalizedString(@"%@ Failed Passcode Attempts", @""), count];
86+
}
87+
self.label.text = text;
88+
89+
[self sizeToFit];
90+
}
91+
92+
#pragma mark - Background Image Managements -
93+
94+
- (void)setBackgroundImageIfNeeded
95+
{
96+
// Don't bother if we're not in a view
97+
if (self.superview == nil) { return; }
98+
99+
// Compare the view height and don't proceed if
100+
if (lround(self.image.size.height) == lround(self.frame.size.height)) { return; }
101+
102+
// Create the image
103+
self.image = [[self class] roundedBackgroundImageWithHeight:self.frame.size.height];
104+
}
105+
106+
+ (UIImage *)roundedBackgroundImageWithHeight:(CGFloat)height
107+
{
108+
UIImage *image = nil;
109+
CGRect frame = CGRectZero;
110+
frame.size.width = height + 1.0;
111+
frame.size.height = height;
112+
113+
UIGraphicsBeginImageContextWithOptions(frame.size, NO, 0.0f);
114+
{
115+
UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:frame cornerRadius:height * 0.5f];
116+
[[UIColor blackColor] setFill];
117+
[path fill];
118+
119+
image = UIGraphicsGetImageFromCurrentImageContext();
120+
}
121+
UIGraphicsEndImageContext();
122+
123+
CGFloat halfHeight = height * 0.5f;
124+
UIEdgeInsets insets = UIEdgeInsetsMake(halfHeight, halfHeight, halfHeight, halfHeight);
125+
image = [image resizableImageWithCapInsets:insets];
126+
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
127+
return image;
128+
}
129+
130+
@end

TOPasscodeViewController/Views/TOPasscodeView.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ - (instancetype)initWithStyle:(TOPasscodeViewStyle)style
3939
if (self = [super initWithFrame:CGRectMake(0,0,320,393)]) {
4040
_style = style;
4141
[self setUp];
42-
4342
}
4443

4544
return self;

TOPasscodeViewControllerExample.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
221ED2FC1EF6FE0400763AD5 /* TOPasscodeSettingsKeypadView.m in Sources */ = {isa = PBXBuildFile; fileRef = 221ED2FB1EF6FE0400763AD5 /* TOPasscodeSettingsKeypadView.m */; };
1313
22600A961EFB681200019ED9 /* TOPasscodeSettingsKeypadButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 22600A951EFB681200019ED9 /* TOPasscodeSettingsKeypadButton.m */; };
1414
226EFC031EE3FA490033B079 /* SettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 226EFC021EE3FA490033B079 /* SettingsViewController.m */; };
15+
226FD7E11F0463EE0010B5AA /* TOPasscodeSettingsWarningLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 226FD7E01F0463EE0010B5AA /* TOPasscodeSettingsWarningLabel.m */; };
1516
2271931D1EE3E0D4000324B7 /* TOBlurView.m in Sources */ = {isa = PBXBuildFile; fileRef = 2271931C1EE3E0D4000324B7 /* TOBlurView.m */; };
1617
2296607C1EEBE1A10064C581 /* TOPasscodeButtonLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 2296607B1EEBE1A10064C581 /* TOPasscodeButtonLabel.m */; };
1718
22D3611E1EDCC1CC002E5D35 /* TOPasscodeCircleImage.m in Sources */ = {isa = PBXBuildFile; fileRef = 22D3610A1EDCC1CC002E5D35 /* TOPasscodeCircleImage.m */; };
@@ -55,6 +56,8 @@
5556
22600A951EFB681200019ED9 /* TOPasscodeSettingsKeypadButton.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TOPasscodeSettingsKeypadButton.m; sourceTree = "<group>"; };
5657
226EFC011EE3FA490033B079 /* SettingsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SettingsViewController.h; sourceTree = "<group>"; };
5758
226EFC021EE3FA490033B079 /* SettingsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SettingsViewController.m; sourceTree = "<group>"; };
59+
226FD7DF1F0463EE0010B5AA /* TOPasscodeSettingsWarningLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TOPasscodeSettingsWarningLabel.h; sourceTree = "<group>"; };
60+
226FD7E01F0463EE0010B5AA /* TOPasscodeSettingsWarningLabel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TOPasscodeSettingsWarningLabel.m; sourceTree = "<group>"; };
5861
2271931B1EE3E0D4000324B7 /* TOBlurView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TOBlurView.h; sourceTree = "<group>"; };
5962
2271931C1EE3E0D4000324B7 /* TOBlurView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TOBlurView.m; sourceTree = "<group>"; };
6063
2296607A1EEBE1A10064C581 /* TOPasscodeButtonLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TOPasscodeButtonLabel.h; sourceTree = "<group>"; };
@@ -183,6 +186,8 @@
183186
22600A951EFB681200019ED9 /* TOPasscodeSettingsKeypadButton.m */,
184187
221ED2FA1EF6FE0400763AD5 /* TOPasscodeSettingsKeypadView.h */,
185188
221ED2FB1EF6FE0400763AD5 /* TOPasscodeSettingsKeypadView.m */,
189+
226FD7DF1F0463EE0010B5AA /* TOPasscodeSettingsWarningLabel.h */,
190+
226FD7E01F0463EE0010B5AA /* TOPasscodeSettingsWarningLabel.m */,
186191
);
187192
path = Views;
188193
sourceTree = "<group>";
@@ -353,6 +358,7 @@
353358
22D361251EDCC1CC002E5D35 /* TOPasscodeView.m in Sources */,
354359
22D3613A1EDCC1D7002E5D35 /* ViewController.m in Sources */,
355360
22D361211EDCC1CC002E5D35 /* TOPasscodeCircleButton.m in Sources */,
361+
226FD7E11F0463EE0010B5AA /* TOPasscodeSettingsWarningLabel.m in Sources */,
356362
22D361261EDCC1CC002E5D35 /* TOPasscodeViewContentLayout.m in Sources */,
357363
2296607C1EEBE1A10064C581 /* TOPasscodeButtonLabel.m in Sources */,
358364
22D361231EDCC1CC002E5D35 /* TOPasscodeNumberInputView.m in Sources */,

0 commit comments

Comments
 (0)