123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- //
- // YMCaptchaTextCell.m
- // MSYOUPAI
- //
- // Created by YoMi on 2023/5/5.
- //
- #import "YMCaptchaTextCell.h"
- #import "YMCaptchaTextLineView.h"
- @interface YMCaptchaTextCell ()
- {
-
- }
- @property (strong, nonatomic) UILabel *valueLabel;
- @property (strong, nonatomic) CABasicAnimation *opacityAnimation;
- @property (strong, nonatomic) UIView *customSecurityView;
- @property (strong, nonatomic) YMCaptchaTextLineView *lineView;
- @end
- @implementation YMCaptchaTextCell
- - (instancetype)initWithFrame:(CGRect)frame{
- if (self = [super initWithFrame:frame]) {
- [self createUIBase];
- }
-
- return self;
- }
- - (void)initPara{
- self.ifNeedCursor = YES;
- self.userInteractionEnabled = NO;
- }
- - (void)createUIBase{
- [self initPara];
-
- _valueLabel = [UILabel new];
- _valueLabel.font = [UIFont systemFontOfSize:38];
- [self.contentView addSubview:_valueLabel];
- [_valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.offset(0);
- make.centerY.offset(0);
- }];
-
- _cursorView = [UIView new];
- [self.contentView addSubview:_cursorView];
- [_cursorView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.offset(0);
- make.centerY.offset(0);
- }];
-
- [self initCellProperty];
- }
- - (void)initCellProperty{
- YMCaptchaTextCellProperty *cellProperty = [YMCaptchaTextCellProperty new];
- self.captchaTextCellProperty = cellProperty;
- }
- - (void)valueLabelLoadData{
- _valueLabel.hidden = NO;
- [self hideCustomSecurityView];
-
- // 默认字体配置
- __weak typeof(self) weakSelf = self;
- void (^defaultTextConfig)(void) = ^{
- if (weakSelf.captchaTextCellProperty.cellFont) {
- weakSelf.valueLabel.font = weakSelf.captchaTextCellProperty.cellFont;
- }
-
- if (weakSelf.captchaTextCellProperty.cellTextColor) {
- weakSelf.valueLabel.textColor = weakSelf.captchaTextCellProperty.cellTextColor;
- }
- };
-
- // 占位字符字体配置
- void (^placeholderTextConfig)(void) = ^{
- if (weakSelf.captchaTextCellProperty.cellFont) {
- weakSelf.valueLabel.font = weakSelf.captchaTextCellProperty.cellPlaceholderFont;
- }
-
- if (weakSelf.captchaTextCellProperty.cellTextColor) {
- weakSelf.valueLabel.textColor = weakSelf.captchaTextCellProperty.cellPlaceholderTextColor;
- }
- };
-
- BOOL hasOriginValue = self.captchaTextCellProperty.originValue && self.captchaTextCellProperty.originValue.length > 0;
- if (hasOriginValue) {
- if (self.captchaTextCellProperty.ifShowSecurity) {
- if (self.captchaTextCellProperty.securityType == YMCaptchaTextSecuritySymbolType) {
- _valueLabel.text = self.captchaTextCellProperty.securitySymbol;
- }else if (self.captchaTextCellProperty.securityType == YMCaptchaTextSecurityCustomViewType) {
- _valueLabel.hidden = YES;
- [self showCustomSecurityView];
- }
-
- }else{
- _valueLabel.text = self.captchaTextCellProperty.originValue;
- }
- defaultTextConfig();
- }else{
- BOOL hasPlaceholderText = self.captchaTextCellProperty.cellPlaceholderText && self.captchaTextCellProperty.cellPlaceholderText.length > 0;
- // 有占位字符
- if (hasPlaceholderText) {
- _valueLabel.text = self.captchaTextCellProperty.cellPlaceholderText;
- placeholderTextConfig();
- }
- // 空
- else{
- _valueLabel.text = @"";
- defaultTextConfig();
- }
- }
-
-
- }
- #pragma mark - Custom security view
- - (void)showCustomSecurityView{
- if (!self.customSecurityView.superview) {
- [self.contentView addSubview:self.customSecurityView];
- [self.customSecurityView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.mas_equalTo(UIEdgeInsetsZero);
- }];
- }
-
- self.customSecurityView.alpha = 1;
- }
- - (void)hideCustomSecurityView{
- // Must add this judge. Otherwise _customSecurityView maybe null, and cause error.
- if (_customSecurityView) {
- self.customSecurityView.alpha = 0;
- }
- }
- #pragma mark - Setter & Getter
- - (CABasicAnimation *)opacityAnimation{
- if (!_opacityAnimation) {
- _opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
- _opacityAnimation.fromValue = @(1.0);
- _opacityAnimation.toValue = @(0.0);
- _opacityAnimation.duration = 0.9;
- _opacityAnimation.repeatCount = HUGE_VALF;
- _opacityAnimation.removedOnCompletion = YES;
- _opacityAnimation.fillMode = kCAFillModeForwards;
- _opacityAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
- }
-
- return _opacityAnimation;
- }
- - (void)setSelected:(BOOL)selected{
- if (selected) {
- self.layer.borderColor = self.captchaTextCellProperty.cellBorderColorSelected.CGColor;
- self.backgroundColor = self.captchaTextCellProperty.cellBgColorSelected;
- }else{
- BOOL hasFill = _valueLabel.text.length > 0 ? YES : NO;
- UIColor *cellBorderColor = self.captchaTextCellProperty.cellBorderColorNormal;
- UIColor *cellBackgroundColor = self.captchaTextCellProperty.cellBgColorNormal;
- if (hasFill) {
- if (self.captchaTextCellProperty.cellBorderColorFilled) {
- cellBorderColor = self.captchaTextCellProperty.cellBorderColorFilled;
- }
- if (self.captchaTextCellProperty.cellBgColorFilled) {
- cellBackgroundColor = self.captchaTextCellProperty.cellBgColorFilled;
- }
- }
- self.layer.borderColor = cellBorderColor.CGColor;
- self.backgroundColor = cellBackgroundColor;
- }
-
- if (_lineView) {
- // 未选中
- if (!selected) {
- if (self.captchaTextCellProperty.originValue.length > 0 && _lineView.underlineColorFilled) {
- // 有内容
- _lineView.lineView.backgroundColor = _lineView.underlineColorFilled;
- }else if (_lineView.underlineColorNormal) {
- // 无内容
- _lineView.lineView.backgroundColor = _lineView.underlineColorNormal;
- }else{
- // 默认
- _lineView.lineView.backgroundColor = [UIColor colorWithRed:49/255.0 green:51/255.0 blue:64/255.0 alpha:1];
- }
- }
- // 已选中
- else if (selected && _lineView.underlineColorSelected){
- _lineView.lineView.backgroundColor = _lineView.underlineColorSelected;
- }
- // 默认
- else{
- _lineView.lineView.backgroundColor = [UIColor colorWithRed:49/255.0 green:51/255.0 blue:64/255.0 alpha:1];
- }
-
- _lineView.selected = selected;
- }
-
- if (_ifNeedCursor) {
- if (selected) {
- _cursorView.hidden= NO;
- [_cursorView.layer addAnimation:self.opacityAnimation forKey:YMCaptchaTextCursoryAnimationKey];
- }else{
- _cursorView.hidden= YES;
- [_cursorView.layer removeAnimationForKey:YMCaptchaTextCursoryAnimationKey];
- }
- }else{
- _cursorView.hidden= YES;
- }
- }
- - (void)setCaptchaTextCellProperty:(YMCaptchaTextCellProperty *)captchaTextCellProperty{
- _captchaTextCellProperty = captchaTextCellProperty;
-
- _cursorView.backgroundColor = captchaTextCellProperty.cellCursorColor;
- [_cursorView mas_updateConstraints:^(MASConstraintMaker *make) {
- make.width.mas_equalTo(captchaTextCellProperty.cellCursorWidth);
- make.height.mas_equalTo(captchaTextCellProperty.cellCursorHeight);
- }];
- self.layer.cornerRadius = captchaTextCellProperty.cornerRadius;
- self.layer.borderWidth = captchaTextCellProperty.borderWidth;
-
- [self valueLabelLoadData];
- }
- - (UIView *)customSecurityView{
- if (!_customSecurityView) {
- // Compatiable for 0.19 verion and earlier.
- if ([self respondsToSelector:@selector(createCustomSecurityView)]) {
- _customSecurityView = [self createCustomSecurityView];
- }
- else if(_captchaTextCellProperty.customSecurityViewBlock){
- NSAssert(_captchaTextCellProperty.customSecurityViewBlock, @"customSecurityViewBlock can not be null!");
- _customSecurityView = _captchaTextCellProperty.customSecurityViewBlock();
- }
- }
-
- return _customSecurityView;
- }
- - (void)layoutSubviews{
- __weak typeof(self) weakSelf = self;
-
- if (_captchaTextCellProperty.showLine && !_lineView) {
- NSAssert(_captchaTextCellProperty.customLineViewBlock, @"customLineViewBlock can not be null!");
- _lineView = _captchaTextCellProperty.customLineViewBlock();
- [self.contentView addSubview:_lineView];
- [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.bottom.top.offset(0);
- }];
- }
-
- if (_captchaTextCellProperty.configCellShadowBlock) {
- _captchaTextCellProperty.configCellShadowBlock(weakSelf.layer);
- }
-
- [super layoutSubviews];
- }
- @end
|