RKCodeView.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // RKCodeView.m
  3. // YBSoul
  4. //
  5. // Created by YB007 on 2021/3/9.
  6. //
  7. #import "RKCodeView.h"
  8. @interface RKCodeView()
  9. @property (nonatomic, strong) UILabel *codeLabel;
  10. @property (nonatomic, strong) UIView *codeEdgeView;
  11. @property (nonatomic, strong) UIView *cursor;
  12. @end
  13. @implementation RKCodeView
  14. - (instancetype)init {
  15. self = [super init];
  16. if (self) {
  17. [self config];
  18. }
  19. return self;
  20. }
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. [self config];
  25. }
  26. return self;
  27. }
  28. - (void)config {
  29. self.userInteractionEnabled = NO;
  30. _codeEdgeView = [[UIView alloc] init];
  31. _codeEdgeView.userInteractionEnabled = NO;
  32. _codeEdgeView.backgroundColor = UIColor.clearColor;
  33. _codeEdgeView.layer.borderWidth = 1;
  34. _codeEdgeView.layer.borderColor = UIColor.clearColor.CGColor;
  35. _codeEdgeView.layer.cornerRadius = 5;
  36. _codeEdgeView.backgroundColor = RGB_COLOR(@"#f6f6f6", 1);
  37. [self addSubview:_codeEdgeView];
  38. _codeLabel = [[UILabel alloc] init];
  39. _codeLabel.font = [UIFont boldSystemFontOfSize:15];
  40. _codeLabel.textColor = UIColor.blackColor;
  41. [self addSubview:_codeLabel];
  42. //默认关闭
  43. _showCursor = NO;
  44. }
  45. - (void)setUiStyle:(CodeUIStyle)uiStyle {
  46. _uiStyle = uiStyle;
  47. if (_uiStyle == CodeUIStyle_Line) {
  48. _codeEdgeView.backgroundColor = UIColor.clearColor;
  49. _codeEdgeView.layer.borderColor = UIColor.whiteColor.CGColor;
  50. }else{
  51. _codeEdgeView.backgroundColor = RGB_COLOR(@"#f6f6f6", 1);
  52. _codeEdgeView.layer.borderColor = UIColor.clearColor.CGColor;
  53. }
  54. }
  55. - (void)setTextFont:(UIFont *)textFont {
  56. _textFont = textFont;
  57. _codeLabel.font = _textFont;
  58. }
  59. - (void)setTextCor:(UIColor *)textCor {
  60. _textCor = textCor;
  61. _codeLabel.textColor = _textCor;
  62. }
  63. - (void)layoutSubviews {
  64. [super layoutSubviews];
  65. [self updateCodeView];
  66. }
  67. -(void)updateCodeView {
  68. CGFloat edgeSize = MIN(self.frame.size.width, self.frame.size.height);
  69. CGFloat edgeX = (self.frame.size.width - edgeSize)/2;
  70. CGFloat edgeY = (self.frame.size.height - edgeSize)/2;
  71. self.codeEdgeView.frame = CGRectMake(edgeX, edgeY, edgeSize, edgeSize);
  72. CGFloat codeHeight = edgeSize *0.8;
  73. CGFloat codeX = (self.frame.size.width - self.codeLabel.frame.size.width) / 2.0;
  74. CGFloat codeY = (self.frame.size.height - codeHeight)/2;
  75. self.codeLabel.frame = CGRectMake(codeX, codeY, self.codeLabel.frame.size.width, codeHeight);
  76. [self updateCursorFrame];
  77. }
  78. - (void)setText:(NSString *)text {
  79. _text = text;
  80. NSLog(@"real:%@",text);
  81. if (_text.length > 0) {
  82. // _codeEdgeView.layer.borderColor = UIColor.redColor.CGColor;
  83. } else {
  84. // _codeEdgeView.layer.borderColor = UIColor.grayColor.CGColor;
  85. }
  86. // 密文处理
  87. if (_secureTextEntry) {
  88. if (text.length>0) {
  89. _codeLabel.text = text;
  90. [self refreshCodeLabel];
  91. }
  92. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  93. if (text.length>0) {
  94. _codeLabel.text = @"●";
  95. }else{
  96. _codeLabel.text = @"";
  97. }
  98. [self refreshCodeLabel];
  99. });
  100. }else{
  101. _codeLabel.text = text;
  102. [self refreshCodeLabel];
  103. }
  104. }
  105. -(void)refreshCodeLabel {
  106. [self.codeLabel sizeToFit];
  107. [self setNeedsLayout];
  108. [self layoutIfNeeded];
  109. }
  110. - (void)updateCursorFrame {
  111. CGFloat x = 0;
  112. if (self.codeLabel.frame.size.width <= 0) {
  113. x = (self.frame.size.width - 1.6) / 2.0;
  114. } else {
  115. x = CGRectGetMaxX(self.codeLabel.frame);
  116. }
  117. CGFloat curHeight = self.frame.size.height *0.45;
  118. CGFloat curY = (self.frame.size.height - curHeight)/2;
  119. //_cursor.frame = CGRectMake(x, 10, 1.6, self.frame.size.height - 20);
  120. _cursor.frame = CGRectMake(x, curY, 1.6, curHeight);
  121. }
  122. - (void)setShowCursor:(BOOL)showCursor {
  123. if (_showCursor == YES && showCursor == YES) { //重复开始, 那么,什么也不做
  124. } else if (_showCursor == YES && showCursor == NO) { //原来是开始的, 现在要求关闭, 那么,就关闭
  125. [_cursor removeFromSuperview];
  126. } else if (_showCursor == NO && showCursor == YES) { //原来是关闭, 现在要求开始, 那么, 开始
  127. _cursor = [[UIView alloc] init];
  128. _cursor.userInteractionEnabled = NO;
  129. _cursor.backgroundColor = Pink_Cor;
  130. [self addSubview:_cursor];
  131. [self updateCursorFrame];
  132. _cursor.alpha = 0;
  133. [self animationOne:_cursor];
  134. } else if (_showCursor == NO && showCursor == NO) { //重复关闭
  135. [_cursor removeFromSuperview];
  136. }
  137. _showCursor = showCursor;
  138. }
  139. // 光标效果
  140. - (void)animationOne:(UIView *)aView {
  141. [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  142. aView.alpha = 1;
  143. } completion:^(BOOL finished) {
  144. if (self.showCursor) {
  145. [self performSelector:@selector(animationTwo:) withObject:aView afterDelay:0.5];
  146. }
  147. }];
  148. }
  149. - (void)animationTwo:(UIView *)aView {
  150. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  151. aView.alpha = 0;
  152. } completion:^(BOOL finished) {
  153. if (self.showCursor) {
  154. [self performSelector:@selector(animationOne:) withObject:aView afterDelay:0.1];
  155. }
  156. }];
  157. }
  158. @end