developer
2023-05-20 e12c7b4c22df631ebdcd16b2f98fbef8f738f92f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
//  CRBoxInputCell.m
//  CaiShenYe
//
//  Created by Chobits on 2019/1/3.
//  Copyright © 2019 Chobits. All rights reserved.
//
 
#import "CRBoxInputCell.h"
#import <Masonry/Masonry.h>
 
@interface CRBoxInputCell ()
{
    
}
 
@property (strong, nonatomic) UILabel *valueLabel;
@property (strong, nonatomic) CABasicAnimation *opacityAnimation;
@property (strong, nonatomic) UIView *customSecurityView;
 
@property (strong, nonatomic) UIView *lineView;
 
@end
 
@implementation CRBoxInputCell
 
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        [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
{
    CRBoxInputCellProperty *cellProperty = [CRBoxInputCellProperty new];
    self.boxInputCellProperty = cellProperty;
}
 
- (void)valueLabelLoadData
{
    _valueLabel.hidden = NO;
    [self hideCustomSecurityView];
    
    BOOL hasOriginValue = self.boxInputCellProperty.originValue && self.boxInputCellProperty.originValue.length > 0;
    if (hasOriginValue) {
        if (self.boxInputCellProperty.ifShowSecurity) {
            if (self.boxInputCellProperty.securityType == CRBoxSecuritySymbolType) {
                _valueLabel.text = self.boxInputCellProperty.securitySymbol;
            }else if (self.boxInputCellProperty.securityType == CRBoxSecurityCustomViewType) {
                _valueLabel.hidden = YES;
                [self showCustomSecurityView];
            }
            
        }else{
            _valueLabel.text = self.boxInputCellProperty.originValue;
        }
    }else{
        _valueLabel.text = @"";
    }
}
 
#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.boxInputCellProperty.cellBorderColorSelected.CGColor;
        self.backgroundColor = self.boxInputCellProperty.cellBgColorSelected;
    }else{
        BOOL hasFill = _valueLabel.text.length > 0 ? YES : NO;
        UIColor *cellBorderColor = self.boxInputCellProperty.cellBorderColorNormal;
        UIColor *cellBackgroundColor = self.boxInputCellProperty.cellBgColorNormal;
        if (hasFill) {
            if (self.boxInputCellProperty.cellBorderColorFilled) {
                cellBorderColor = self.boxInputCellProperty.cellBorderColorFilled;
            }
            if (self.boxInputCellProperty.cellBgColorFilled) {
                cellBackgroundColor = self.boxInputCellProperty.cellBgColorFilled;
            }
        }
        self.layer.borderColor = cellBorderColor.CGColor;
        self.backgroundColor = cellBackgroundColor;
    }
    
    if (_ifNeedCursor) {
        if (selected) {
            _cursorView.hidden= NO;
            [_cursorView.layer addAnimation:self.opacityAnimation forKey:CRBoxCursoryAnimationKey];
        }else{
            _cursorView.hidden= YES;
            [_cursorView.layer removeAnimationForKey:CRBoxCursoryAnimationKey];
        }
    }else{
        _cursorView.hidden= YES;
    }
}
 
- (void)setBoxInputCellProperty:(CRBoxInputCellProperty *)boxInputCellProperty
{
    _boxInputCellProperty = boxInputCellProperty;
    
    _cursorView.backgroundColor = boxInputCellProperty.cellCursorColor;
    [_cursorView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(boxInputCellProperty.cellCursorWidth);
        make.height.mas_equalTo(boxInputCellProperty.cellCursorHeight);
    }];
    self.layer.cornerRadius = boxInputCellProperty.cornerRadius;
    self.layer.borderWidth = boxInputCellProperty.borderWidth;
    
    if (boxInputCellProperty.cellFont) {
        _valueLabel.font = boxInputCellProperty.cellFont;
    }
    
    if (boxInputCellProperty.cellTextColor) {
        _valueLabel.textColor = boxInputCellProperty.cellTextColor;
    }
    
    [self valueLabelLoadData];
}
 
- (UIView *)customSecurityView
{
    if (!_customSecurityView) {
        // Compatiable for 0.19 verion and earlier.
        if ([self respondsToSelector:@selector(createCustomSecurityView)]) {
            _customSecurityView = [self createCustomSecurityView];
        }
        else if(_boxInputCellProperty.customSecurityViewBlock){
            NSAssert(_boxInputCellProperty.customSecurityViewBlock, @"customSecurityViewBlock can not be null!");
            _customSecurityView = _boxInputCellProperty.customSecurityViewBlock();
        }
    }
    
    return _customSecurityView;
}
 
- (void)layoutSubviews
{
    __weak typeof(self) weakSelf = self;
    
    if (_boxInputCellProperty.showLine && !_lineView) {
        NSAssert(_boxInputCellProperty.customLineViewBlock, @"customLineViewBlock can not be null!");
        _lineView = _boxInputCellProperty.customLineViewBlock();
        [self.contentView addSubview:_lineView];
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.offset(0);
        }];
    }
    
    if (_boxInputCellProperty.configCellShadowBlock) {
        _boxInputCellProperty.configCellShadowBlock(weakSelf.layer);
    }
    
    [super layoutSubviews];
}
 
@end