Files
CRBoxInputView/PodCode/Classes/CRBoxInputCell.m

282 lines
8.7 KiB
Mathematica
Raw Permalink Normal View History

2019-01-03 18:03:44 +08:00
//
// CRBoxInputCell.m
// CaiShenYe
//
// Created by Chobits on 2019/1/3.
// Copyright © 2019 Chobits. All rights reserved.
//
#import "CRBoxInputCell.h"
#import <Masonry/Masonry.h>
2019-08-01 17:08:55 +08:00
#import "CRLineView.h"
2019-01-03 18:03:44 +08:00
@interface CRBoxInputCell ()
{
}
2019-01-04 15:28:43 +08:00
@property (strong, nonatomic) UILabel *valueLabel;
@property (strong, nonatomic) CABasicAnimation *opacityAnimation;
2019-01-06 18:59:10 +08:00
@property (strong, nonatomic) UIView *customSecurityView;
2019-01-04 15:28:43 +08:00
2019-08-01 17:08:55 +08:00
@property (strong, nonatomic) CRLineView *lineView;
2019-06-10 20:10:20 +08:00
2019-01-03 18:03:44 +08:00
@end
@implementation CRBoxInputCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
2019-01-07 14:46:50 +08:00
[self createUIBase];
2019-01-03 18:03:44 +08:00
}
return self;
}
2019-01-04 15:28:43 +08:00
- (void)initPara
2019-01-03 18:03:44 +08:00
{
self.ifNeedCursor = YES;
self.userInteractionEnabled = NO;
2019-01-04 15:28:43 +08:00
}
2019-01-07 14:46:50 +08:00
- (void)createUIBase
2019-01-04 15:28:43 +08:00
{
[self initPara];
2019-01-03 18:03:44 +08:00
_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);
2019-01-07 12:30:15 +08:00
make.centerY.offset(0);
2019-01-03 18:03:44 +08:00
}];
[self initCellProperty];
}
- (void)initCellProperty
{
CRBoxInputCellProperty *cellProperty = [CRBoxInputCellProperty new];
self.boxInputCellProperty = cellProperty;
}
2019-01-04 16:51:30 +08:00
- (void)valueLabelLoadData
{
_valueLabel.hidden = NO;
2019-01-04 20:07:30 +08:00
[self hideCustomSecurityView];
2019-01-04 16:51:30 +08:00
2019-07-16 14:59:37 +08:00
//
__weak typeof(self) weakSelf = self;
void (^defaultTextConfig)(void) = ^{
if (weakSelf.boxInputCellProperty.cellFont) {
weakSelf.valueLabel.font = weakSelf.boxInputCellProperty.cellFont;
}
if (weakSelf.boxInputCellProperty.cellTextColor) {
weakSelf.valueLabel.textColor = weakSelf.boxInputCellProperty.cellTextColor;
}
};
//
void (^placeholderTextConfig)(void) = ^{
if (weakSelf.boxInputCellProperty.cellFont) {
weakSelf.valueLabel.font = weakSelf.boxInputCellProperty.cellPlaceholderFont;
}
if (weakSelf.boxInputCellProperty.cellTextColor) {
weakSelf.valueLabel.textColor = weakSelf.boxInputCellProperty.cellPlaceholderTextColor;
}
};
2019-01-04 20:07:30 +08:00
BOOL hasOriginValue = self.boxInputCellProperty.originValue && self.boxInputCellProperty.originValue.length > 0;
if (hasOriginValue) {
2019-01-04 16:51:30 +08:00
if (self.boxInputCellProperty.ifShowSecurity) {
2019-01-07 15:50:20 +08:00
if (self.boxInputCellProperty.securityType == CRBoxSecuritySymbolType) {
2019-01-04 16:51:30 +08:00
_valueLabel.text = self.boxInputCellProperty.securitySymbol;
2019-01-07 15:50:20 +08:00
}else if (self.boxInputCellProperty.securityType == CRBoxSecurityCustomViewType) {
2019-01-04 16:51:30 +08:00
_valueLabel.hidden = YES;
2019-01-04 18:43:36 +08:00
[self showCustomSecurityView];
2019-01-04 16:51:30 +08:00
}
}else{
_valueLabel.text = self.boxInputCellProperty.originValue;
}
2019-07-16 14:59:37 +08:00
defaultTextConfig();
2019-01-04 16:51:30 +08:00
}else{
2019-07-16 14:59:37 +08:00
BOOL hasPlaceholderText = self.boxInputCellProperty.cellPlaceholderText && self.boxInputCellProperty.cellPlaceholderText.length > 0;
2019-07-16 15:07:08 +08:00
//
2019-07-16 14:41:42 +08:00
if (hasPlaceholderText) {
2019-07-16 14:59:37 +08:00
_valueLabel.text = self.boxInputCellProperty.cellPlaceholderText;
placeholderTextConfig();
2019-07-16 15:07:08 +08:00
}
//
else{
2019-07-16 14:41:42 +08:00
_valueLabel.text = @"";
2019-07-16 14:59:37 +08:00
defaultTextConfig();
2019-07-16 14:41:42 +08:00
}
2019-01-04 16:51:30 +08:00
}
2019-07-16 14:59:37 +08:00
2019-01-04 16:51:30 +08:00
}
#pragma mark - Custom security view
2019-01-04 18:43:36 +08:00
- (void)showCustomSecurityView
2019-01-04 16:51:30 +08:00
{
2019-01-04 18:43:36 +08:00
if (!self.customSecurityView.superview) {
[self.contentView addSubview:self.customSecurityView];
[self.customSecurityView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsZero);
}];
2019-01-04 16:51:30 +08:00
}
2019-01-04 18:43:36 +08:00
self.customSecurityView.alpha = 1;
2019-01-04 16:51:30 +08:00
}
2019-01-04 18:43:36 +08:00
- (void)hideCustomSecurityView
{
2019-06-10 22:39:57 +08:00
// Must add this judge. Otherwise _customSecurityView maybe null, and cause error.
if (_customSecurityView) {
self.customSecurityView.alpha = 0;
}
2019-01-04 16:51:30 +08:00
}
2019-01-03 18:03:44 +08:00
#pragma mark - Setter & Getter
2019-01-04 16:51:30 +08:00
- (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;
}
2019-01-03 18:03:44 +08:00
- (void)setSelected:(BOOL)selected
{
if (selected) {
self.layer.borderColor = self.boxInputCellProperty.cellBorderColorSelected.CGColor;
2019-01-07 12:56:47 +08:00
self.backgroundColor = self.boxInputCellProperty.cellBgColorSelected;
2019-01-03 18:03:44 +08:00
}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;
2019-01-03 18:03:44 +08:00
}
2019-08-01 17:08:55 +08:00
if (_lineView) {
2020-01-19 17:09:33 +08:00
//
if (!selected) {
if (self.boxInputCellProperty.originValue.length > 0 && _lineView.underlineColorFilled) {
//
_lineView.lineView.backgroundColor = _lineView.underlineColorFilled;
}else if (_lineView.underlineColorNormal) {
//
_lineView.lineView.backgroundColor = _lineView.underlineColorNormal;
}else{
//
_lineView.lineView.backgroundColor = CRColorMaster;
}
}
//
else if (selected && _lineView.underlineColorSelected){
2019-08-01 17:48:07 +08:00
_lineView.lineView.backgroundColor = _lineView.underlineColorSelected;
2020-01-19 17:09:33 +08:00
}
//
else{
2019-08-01 17:41:36 +08:00
_lineView.lineView.backgroundColor = CRColorMaster;
2019-08-01 17:08:55 +08:00
}
_lineView.selected = selected;
2019-08-01 17:08:55 +08:00
}
2019-01-03 18:03:44 +08:00
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;
2019-01-07 12:30:15 +08:00
[_cursorView mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(boxInputCellProperty.cellCursorWidth);
make.height.mas_equalTo(boxInputCellProperty.cellCursorHeight);
}];
2019-01-03 18:03:44 +08:00
self.layer.cornerRadius = boxInputCellProperty.cornerRadius;
self.layer.borderWidth = boxInputCellProperty.borderWidth;
2019-01-04 15:28:43 +08:00
[self valueLabelLoadData];
}
2019-01-04 16:51:30 +08:00
- (UIView *)customSecurityView
2019-01-03 18:03:44 +08:00
{
2019-01-04 16:51:30 +08:00
if (!_customSecurityView) {
2019-06-11 08:47:39 +08:00
// Compatiable for 0.19 verion and earlier.
if ([self respondsToSelector:@selector(createCustomSecurityView)]) {
_customSecurityView = [self createCustomSecurityView];
}
else if(_boxInputCellProperty.customSecurityViewBlock){
2019-06-11 08:55:00 +08:00
NSAssert(_boxInputCellProperty.customSecurityViewBlock, @"customSecurityViewBlock can not be null");
_customSecurityView = _boxInputCellProperty.customSecurityViewBlock();
2019-06-10 20:10:20 +08:00
}
2019-01-03 18:03:44 +08:00
}
2019-01-04 16:51:30 +08:00
return _customSecurityView;
2019-01-03 18:03:44 +08:00
}
2019-06-10 19:23:37 +08:00
- (void)layoutSubviews
{
2019-06-10 23:34:10 +08:00
__weak typeof(self) weakSelf = self;
2019-06-10 22:47:05 +08:00
if (_boxInputCellProperty.showLine && !_lineView) {
NSAssert(_boxInputCellProperty.customLineViewBlock, @"customLineViewBlock can not be null");
2019-06-10 20:10:20 +08:00
_lineView = _boxInputCellProperty.customLineViewBlock();
[self.contentView addSubview:_lineView];
[_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
2019-06-11 08:10:58 +08:00
make.left.right.bottom.top.offset(0);
2019-06-10 20:10:20 +08:00
}];
}
2019-06-10 23:34:10 +08:00
if (_boxInputCellProperty.configCellShadowBlock) {
_boxInputCellProperty.configCellShadowBlock(weakSelf.layer);
}
2019-06-10 19:23:37 +08:00
[super layoutSubviews];
}
2019-01-03 18:03:44 +08:00
@end