YMCaptchaTextCell.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // YMCaptchaTextCell.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2023/5/5.
  6. //
  7. #import "YMCaptchaTextCell.h"
  8. #import "YMCaptchaTextLineView.h"
  9. @interface YMCaptchaTextCell ()
  10. {
  11. }
  12. @property (strong, nonatomic) UILabel *valueLabel;
  13. @property (strong, nonatomic) CABasicAnimation *opacityAnimation;
  14. @property (strong, nonatomic) UIView *customSecurityView;
  15. @property (strong, nonatomic) YMCaptchaTextLineView *lineView;
  16. @end
  17. @implementation YMCaptchaTextCell
  18. - (instancetype)initWithFrame:(CGRect)frame{
  19. if (self = [super initWithFrame:frame]) {
  20. [self createUIBase];
  21. }
  22. return self;
  23. }
  24. - (void)initPara{
  25. self.ifNeedCursor = YES;
  26. self.userInteractionEnabled = NO;
  27. }
  28. - (void)createUIBase{
  29. [self initPara];
  30. _valueLabel = [UILabel new];
  31. _valueLabel.font = [UIFont systemFontOfSize:38];
  32. [self.contentView addSubview:_valueLabel];
  33. [_valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.centerX.offset(0);
  35. make.centerY.offset(0);
  36. }];
  37. _cursorView = [UIView new];
  38. [self.contentView addSubview:_cursorView];
  39. [_cursorView mas_makeConstraints:^(MASConstraintMaker *make) {
  40. make.centerX.offset(0);
  41. make.centerY.offset(0);
  42. }];
  43. [self initCellProperty];
  44. }
  45. - (void)initCellProperty{
  46. YMCaptchaTextCellProperty *cellProperty = [YMCaptchaTextCellProperty new];
  47. self.captchaTextCellProperty = cellProperty;
  48. }
  49. - (void)valueLabelLoadData{
  50. _valueLabel.hidden = NO;
  51. [self hideCustomSecurityView];
  52. // 默认字体配置
  53. __weak typeof(self) weakSelf = self;
  54. void (^defaultTextConfig)(void) = ^{
  55. if (weakSelf.captchaTextCellProperty.cellFont) {
  56. weakSelf.valueLabel.font = weakSelf.captchaTextCellProperty.cellFont;
  57. }
  58. if (weakSelf.captchaTextCellProperty.cellTextColor) {
  59. weakSelf.valueLabel.textColor = weakSelf.captchaTextCellProperty.cellTextColor;
  60. }
  61. };
  62. // 占位字符字体配置
  63. void (^placeholderTextConfig)(void) = ^{
  64. if (weakSelf.captchaTextCellProperty.cellFont) {
  65. weakSelf.valueLabel.font = weakSelf.captchaTextCellProperty.cellPlaceholderFont;
  66. }
  67. if (weakSelf.captchaTextCellProperty.cellTextColor) {
  68. weakSelf.valueLabel.textColor = weakSelf.captchaTextCellProperty.cellPlaceholderTextColor;
  69. }
  70. };
  71. BOOL hasOriginValue = self.captchaTextCellProperty.originValue && self.captchaTextCellProperty.originValue.length > 0;
  72. if (hasOriginValue) {
  73. if (self.captchaTextCellProperty.ifShowSecurity) {
  74. if (self.captchaTextCellProperty.securityType == YMCaptchaTextSecuritySymbolType) {
  75. _valueLabel.text = self.captchaTextCellProperty.securitySymbol;
  76. }else if (self.captchaTextCellProperty.securityType == YMCaptchaTextSecurityCustomViewType) {
  77. _valueLabel.hidden = YES;
  78. [self showCustomSecurityView];
  79. }
  80. }else{
  81. _valueLabel.text = self.captchaTextCellProperty.originValue;
  82. }
  83. defaultTextConfig();
  84. }else{
  85. BOOL hasPlaceholderText = self.captchaTextCellProperty.cellPlaceholderText && self.captchaTextCellProperty.cellPlaceholderText.length > 0;
  86. // 有占位字符
  87. if (hasPlaceholderText) {
  88. _valueLabel.text = self.captchaTextCellProperty.cellPlaceholderText;
  89. placeholderTextConfig();
  90. }
  91. // 空
  92. else{
  93. _valueLabel.text = @"";
  94. defaultTextConfig();
  95. }
  96. }
  97. }
  98. #pragma mark - Custom security view
  99. - (void)showCustomSecurityView{
  100. if (!self.customSecurityView.superview) {
  101. [self.contentView addSubview:self.customSecurityView];
  102. [self.customSecurityView mas_makeConstraints:^(MASConstraintMaker *make) {
  103. make.edges.mas_equalTo(UIEdgeInsetsZero);
  104. }];
  105. }
  106. self.customSecurityView.alpha = 1;
  107. }
  108. - (void)hideCustomSecurityView{
  109. // Must add this judge. Otherwise _customSecurityView maybe null, and cause error.
  110. if (_customSecurityView) {
  111. self.customSecurityView.alpha = 0;
  112. }
  113. }
  114. #pragma mark - Setter & Getter
  115. - (CABasicAnimation *)opacityAnimation{
  116. if (!_opacityAnimation) {
  117. _opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  118. _opacityAnimation.fromValue = @(1.0);
  119. _opacityAnimation.toValue = @(0.0);
  120. _opacityAnimation.duration = 0.9;
  121. _opacityAnimation.repeatCount = HUGE_VALF;
  122. _opacityAnimation.removedOnCompletion = YES;
  123. _opacityAnimation.fillMode = kCAFillModeForwards;
  124. _opacityAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
  125. }
  126. return _opacityAnimation;
  127. }
  128. - (void)setSelected:(BOOL)selected{
  129. if (selected) {
  130. self.layer.borderColor = self.captchaTextCellProperty.cellBorderColorSelected.CGColor;
  131. self.backgroundColor = self.captchaTextCellProperty.cellBgColorSelected;
  132. }else{
  133. BOOL hasFill = _valueLabel.text.length > 0 ? YES : NO;
  134. UIColor *cellBorderColor = self.captchaTextCellProperty.cellBorderColorNormal;
  135. UIColor *cellBackgroundColor = self.captchaTextCellProperty.cellBgColorNormal;
  136. if (hasFill) {
  137. if (self.captchaTextCellProperty.cellBorderColorFilled) {
  138. cellBorderColor = self.captchaTextCellProperty.cellBorderColorFilled;
  139. }
  140. if (self.captchaTextCellProperty.cellBgColorFilled) {
  141. cellBackgroundColor = self.captchaTextCellProperty.cellBgColorFilled;
  142. }
  143. }
  144. self.layer.borderColor = cellBorderColor.CGColor;
  145. self.backgroundColor = cellBackgroundColor;
  146. }
  147. if (_lineView) {
  148. // 未选中
  149. if (!selected) {
  150. if (self.captchaTextCellProperty.originValue.length > 0 && _lineView.underlineColorFilled) {
  151. // 有内容
  152. _lineView.lineView.backgroundColor = _lineView.underlineColorFilled;
  153. }else if (_lineView.underlineColorNormal) {
  154. // 无内容
  155. _lineView.lineView.backgroundColor = _lineView.underlineColorNormal;
  156. }else{
  157. // 默认
  158. _lineView.lineView.backgroundColor = [UIColor colorWithRed:49/255.0 green:51/255.0 blue:64/255.0 alpha:1];
  159. }
  160. }
  161. // 已选中
  162. else if (selected && _lineView.underlineColorSelected){
  163. _lineView.lineView.backgroundColor = _lineView.underlineColorSelected;
  164. }
  165. // 默认
  166. else{
  167. _lineView.lineView.backgroundColor = [UIColor colorWithRed:49/255.0 green:51/255.0 blue:64/255.0 alpha:1];
  168. }
  169. _lineView.selected = selected;
  170. }
  171. if (_ifNeedCursor) {
  172. if (selected) {
  173. _cursorView.hidden= NO;
  174. [_cursorView.layer addAnimation:self.opacityAnimation forKey:YMCaptchaTextCursoryAnimationKey];
  175. }else{
  176. _cursorView.hidden= YES;
  177. [_cursorView.layer removeAnimationForKey:YMCaptchaTextCursoryAnimationKey];
  178. }
  179. }else{
  180. _cursorView.hidden= YES;
  181. }
  182. }
  183. - (void)setCaptchaTextCellProperty:(YMCaptchaTextCellProperty *)captchaTextCellProperty{
  184. _captchaTextCellProperty = captchaTextCellProperty;
  185. _cursorView.backgroundColor = captchaTextCellProperty.cellCursorColor;
  186. [_cursorView mas_updateConstraints:^(MASConstraintMaker *make) {
  187. make.width.mas_equalTo(captchaTextCellProperty.cellCursorWidth);
  188. make.height.mas_equalTo(captchaTextCellProperty.cellCursorHeight);
  189. }];
  190. self.layer.cornerRadius = captchaTextCellProperty.cornerRadius;
  191. self.layer.borderWidth = captchaTextCellProperty.borderWidth;
  192. [self valueLabelLoadData];
  193. }
  194. - (UIView *)customSecurityView{
  195. if (!_customSecurityView) {
  196. // Compatiable for 0.19 verion and earlier.
  197. if ([self respondsToSelector:@selector(createCustomSecurityView)]) {
  198. _customSecurityView = [self createCustomSecurityView];
  199. }
  200. else if(_captchaTextCellProperty.customSecurityViewBlock){
  201. NSAssert(_captchaTextCellProperty.customSecurityViewBlock, @"customSecurityViewBlock can not be null!");
  202. _customSecurityView = _captchaTextCellProperty.customSecurityViewBlock();
  203. }
  204. }
  205. return _customSecurityView;
  206. }
  207. - (void)layoutSubviews{
  208. __weak typeof(self) weakSelf = self;
  209. if (_captchaTextCellProperty.showLine && !_lineView) {
  210. NSAssert(_captchaTextCellProperty.customLineViewBlock, @"customLineViewBlock can not be null!");
  211. _lineView = _captchaTextCellProperty.customLineViewBlock();
  212. [self.contentView addSubview:_lineView];
  213. [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
  214. make.left.right.bottom.top.offset(0);
  215. }];
  216. }
  217. if (_captchaTextCellProperty.configCellShadowBlock) {
  218. _captchaTextCellProperty.configCellShadowBlock(weakSelf.layer);
  219. }
  220. [super layoutSubviews];
  221. }
  222. @end