TOPasscodeSettingsWarningLabel.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // TOPasscodeSettingsWarningLabel.m
  3. //
  4. // Copyright 2017 Timothy Oliver. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to
  8. // deal in the Software without restriction, including without limitation the
  9. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. // sell copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  21. // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #import "TOPasscodeSettingsWarningLabel.h"
  23. @interface TOPasscodeSettingsWarningLabel ()
  24. @property (nonatomic, strong) UILabel *label;
  25. @end
  26. @implementation TOPasscodeSettingsWarningLabel
  27. @synthesize backgroundColor = __backgroundColor;
  28. #pragma mark - View Setup -
  29. - (instancetype)initWithFrame:(CGRect)frame
  30. {
  31. if (self = [super initWithFrame:frame]) {
  32. [self setUp];
  33. }
  34. return self;
  35. }
  36. - (void)setUp
  37. {
  38. _numberOfWarnings = 0;
  39. _textPadding = CGSizeMake(14.0f, 6.0f);
  40. self.tintColor = [UIColor colorWithRed:214.0f/255.0f green:63.0f/255.0f blue:63.0f/255.0f alpha:1.0f];
  41. self.label = [[UILabel alloc] initWithFrame:CGRectZero];
  42. self.label.backgroundColor = [UIColor clearColor];
  43. self.label.textAlignment = NSTextAlignmentCenter;
  44. self.label.textColor = [UIColor whiteColor];
  45. self.label.font = [UIFont systemFontOfSize:15.0f];
  46. [self setTextForCount:0];
  47. [self.label sizeToFit];
  48. [self addSubview:self.label];
  49. }
  50. - (void)didMoveToSuperview
  51. {
  52. [super didMoveToSuperview];
  53. [self setBackgroundImageIfNeeded];
  54. }
  55. #pragma mark - View Layout -
  56. - (void)sizeToFit
  57. {
  58. [super sizeToFit];
  59. [self.label sizeToFit];
  60. CGRect labelFrame = self.label.frame;
  61. CGRect frame = self.frame;
  62. labelFrame = CGRectInset(labelFrame, -self.textPadding.width, -self.textPadding.height);
  63. frame.size = labelFrame.size;
  64. self.frame = CGRectIntegral(frame);
  65. }
  66. - (void)layoutSubviews
  67. {
  68. CGRect frame = self.frame;
  69. CGRect labelFrame = self.label.frame;
  70. labelFrame.origin.x = (CGRectGetWidth(frame) - CGRectGetWidth(labelFrame)) * 0.5f;
  71. labelFrame.origin.y = (CGRectGetHeight(frame) - CGRectGetHeight(labelFrame)) * 0.5f;
  72. self.label.frame = labelFrame;
  73. }
  74. #pragma mark - View State Handling -
  75. - (void)setTextForCount:(NSInteger)count
  76. {
  77. NSString *text = nil;
  78. if (count == 1) {
  79. text = NSLocalizedString(@"1 Failed Passcode Attempt", @"");
  80. }
  81. else {
  82. text = [NSString stringWithFormat:NSLocalizedString(@"%d Failed Passcode Attempts", @""), count];
  83. }
  84. self.label.text = text;
  85. [self sizeToFit];
  86. }
  87. #pragma mark - Background Image Managements -
  88. - (void)setBackgroundImageIfNeeded
  89. {
  90. // Don't bother if we're not in a view
  91. if (self.superview == nil) { return; }
  92. // Compare the view height and don't proceed if
  93. if (lround(self.image.size.height) == lround(self.frame.size.height)) { return; }
  94. // Create the image
  95. self.image = [[self class] roundedBackgroundImageWithHeight:self.frame.size.height];
  96. }
  97. + (UIImage *)roundedBackgroundImageWithHeight:(CGFloat)height
  98. {
  99. UIImage *image = nil;
  100. CGRect frame = CGRectZero;
  101. frame.size.width = height + 1.0;
  102. frame.size.height = height;
  103. UIGraphicsBeginImageContextWithOptions(frame.size, NO, 0.0f);
  104. {
  105. UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:frame cornerRadius:height * 0.5f];
  106. [[UIColor blackColor] setFill];
  107. [path fill];
  108. image = UIGraphicsGetImageFromCurrentImageContext();
  109. }
  110. UIGraphicsEndImageContext();
  111. CGFloat halfHeight = height * 0.5f;
  112. UIEdgeInsets insets = UIEdgeInsetsMake(halfHeight, halfHeight, halfHeight, halfHeight);
  113. image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  114. image = [image resizableImageWithCapInsets:insets];
  115. return image;
  116. }
  117. #pragma mark - Accessors -
  118. - (void)setNumberOfWarnings:(NSInteger)numberOfWarnings
  119. {
  120. _numberOfWarnings = numberOfWarnings;
  121. [self setTextForCount:_numberOfWarnings];
  122. }
  123. @end