BKPasscodeField.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // BKPasscodeField.m
  3. // BKPasscodeViewDemo
  4. //
  5. // Created by Byungkook Jang on 2014. 4. 20..
  6. // Copyright (c) 2014년 Byungkook Jang. All rights reserved.
  7. //
  8. #import "BKPasscodeField.h"
  9. @interface BKPasscodeField ()
  10. @property (strong, nonatomic) NSMutableString *mutablePasscode;
  11. @property (strong, nonatomic) NSRegularExpression *nonDigitRegularExpression;
  12. @end
  13. @implementation BKPasscodeField
  14. - (id)initWithFrame:(CGRect)frame
  15. {
  16. self = [super initWithFrame:frame];
  17. if (self) {
  18. [self _initialize];
  19. }
  20. return self;
  21. }
  22. - (id)initWithCoder:(NSCoder *)aDecoder
  23. {
  24. self = [super initWithCoder:aDecoder];
  25. if (self) {
  26. [self _initialize];
  27. }
  28. return self;
  29. }
  30. - (id)init
  31. {
  32. self = [super init];
  33. if (self) {
  34. [self _initialize];
  35. }
  36. return self;
  37. }
  38. - (void)_initialize
  39. {
  40. _maximumLength = 4;
  41. _dotSize = CGSizeMake(18.0f, 19.0f);
  42. _dotSpacing = 25.0f;
  43. _lineHeight = 3.0f;
  44. _dotColor = [UIColor blackColor];
  45. self.backgroundColor = [UIColor clearColor];
  46. _mutablePasscode = [[NSMutableString alloc] initWithCapacity:4];
  47. [self addTarget:self action:@selector(didTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
  48. }
  49. - (NSRegularExpression *)nonDigitRegularExpression
  50. {
  51. if (nil == _nonDigitRegularExpression) {
  52. _nonDigitRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"[^0-9]+" options:0 error:nil];
  53. }
  54. return _nonDigitRegularExpression;
  55. }
  56. - (NSString *)passcode
  57. {
  58. return self.mutablePasscode;
  59. }
  60. - (void)setPasscode:(NSString *)passcode
  61. {
  62. if (passcode) {
  63. if (passcode.length > self.maximumLength) {
  64. passcode = [passcode substringWithRange:NSMakeRange(0, self.maximumLength)];
  65. }
  66. self.mutablePasscode = [NSMutableString stringWithString:passcode];
  67. } else {
  68. self.mutablePasscode = [NSMutableString string];
  69. }
  70. [self setNeedsDisplay];
  71. }
  72. #pragma mark - UIKeyInput
  73. - (BOOL)hasText
  74. {
  75. return (self.mutablePasscode.length > 0);
  76. }
  77. - (void)insertText:(NSString *)text
  78. {
  79. if (self.enabled == NO) {
  80. return;
  81. }
  82. if (self.keyboardType == UIKeyboardTypeNumberPad) {
  83. text = [self.nonDigitRegularExpression stringByReplacingMatchesInString:text options:0 range:NSMakeRange(0, text.length) withTemplate:@""];
  84. }
  85. if (text.length == 0) {
  86. return;
  87. }
  88. NSInteger newLength = self.mutablePasscode.length + text.length;
  89. if (newLength > self.maximumLength) {
  90. return;
  91. }
  92. if ([self.delegate respondsToSelector:@selector(passcodeField:shouldInsertText:)]) {
  93. if (NO == [self.delegate passcodeField:self shouldInsertText:text]) {
  94. return;
  95. }
  96. }
  97. [self.mutablePasscode appendString:text];
  98. [self setNeedsDisplay];
  99. [self sendActionsForControlEvents:UIControlEventEditingChanged];
  100. }
  101. - (void)deleteBackward
  102. {
  103. if (self.enabled == NO) {
  104. return;
  105. }
  106. if ([self.delegate respondsToSelector:@selector(passcodeFieldShouldDeleteBackward:)]) {
  107. if (NO == [self.delegate passcodeFieldShouldDeleteBackward:self]) {
  108. return;
  109. }
  110. }
  111. if (self.mutablePasscode.length == 0) {
  112. return;
  113. }
  114. [self.mutablePasscode deleteCharactersInRange:NSMakeRange(self.mutablePasscode.length - 1, 1)];
  115. [self setNeedsDisplay];
  116. [self sendActionsForControlEvents:UIControlEventEditingChanged];
  117. }
  118. - (UITextAutocapitalizationType)autocapitalizationType
  119. {
  120. return UITextAutocapitalizationTypeNone;
  121. }
  122. - (UITextAutocorrectionType)autocorrectionType
  123. {
  124. return UITextAutocorrectionTypeNo;
  125. }
  126. - (UITextSpellCheckingType)spellCheckingType
  127. {
  128. return UITextSpellCheckingTypeNo;
  129. }
  130. - (BOOL)enablesReturnKeyAutomatically
  131. {
  132. return YES;
  133. }
  134. - (UIKeyboardAppearance)keyboardAppearance
  135. {
  136. return UIKeyboardAppearanceDefault;
  137. }
  138. - (UIReturnKeyType)returnKeyType
  139. {
  140. return UIReturnKeyDone;
  141. }
  142. - (BOOL)isSecureTextEntry
  143. {
  144. return YES;
  145. }
  146. #pragma mark - UIView
  147. - (CGSize)contentSize
  148. {
  149. return CGSizeMake(self.maximumLength * _dotSize.width + (self.maximumLength - 1) * _dotSpacing,
  150. _dotSize.height);
  151. }
  152. - (void)setFrame:(CGRect)frame
  153. {
  154. [super setFrame:frame];
  155. [self setNeedsDisplay];
  156. }
  157. - (void)drawRect:(CGRect)rect
  158. {
  159. CGSize contentSize = [self contentSize];
  160. CGPoint origin = CGPointMake(floorf((self.frame.size.width - contentSize.width) * 0.5f),
  161. floorf((self.frame.size.height - contentSize.height) * 0.5f));
  162. if ([self.imageSource respondsToSelector:@selector(passcodeField:dotImageAtIndex:filled:)]) {
  163. for (NSUInteger i = 0; i < self.maximumLength; i++) {
  164. UIImage *image = nil;
  165. if (i < self.mutablePasscode.length) {
  166. // draw filled image
  167. image = [self.imageSource passcodeField:self dotImageAtIndex:i filled:YES];
  168. } else {
  169. // draw blank image
  170. image = [self.imageSource passcodeField:self dotImageAtIndex:i filled:NO];
  171. }
  172. if (image) {
  173. CGRect imageFrame = CGRectMake(origin.x, origin.y, self.dotSize.width, self.dotSize.height);
  174. [image drawInRect:imageFrame];
  175. }
  176. origin.x += (self.dotSize.width + self.dotSpacing);
  177. }
  178. } else {
  179. CGContextRef context = UIGraphicsGetCurrentContext();
  180. CGContextSetFillColorWithColor(context, self.dotColor.CGColor);
  181. for (NSUInteger i = 0; i < self.maximumLength; i++) {
  182. if (i < self.mutablePasscode.length) {
  183. // draw circle
  184. CGRect circleFrame = CGRectMake(origin.x, origin.y, self.dotSize.width, self.dotSize.height);
  185. CGContextFillEllipseInRect(context, circleFrame);
  186. } else {
  187. // draw line
  188. CGRect lineFrame = CGRectMake(origin.x, origin.y + floorf((self.dotSize.height - self.lineHeight) * 0.5f),
  189. self.dotSize.width, self.lineHeight);
  190. CGContextFillRect(context, lineFrame);
  191. }
  192. origin.x += (self.dotSize.width + self.dotSpacing);
  193. }
  194. }
  195. }
  196. - (CGSize)sizeThatFits:(CGSize)size
  197. {
  198. return [self contentSize];
  199. }
  200. #pragma mark - UIResponder
  201. - (BOOL)canBecomeFirstResponder
  202. {
  203. return YES;
  204. }
  205. #pragma mark - Actions
  206. - (void)didTouchUpInside:(id)sender
  207. {
  208. [self becomeFirstResponder];
  209. }
  210. @end