// // KBConsumptionRecordCell.m // keyBoard // #import "KBConsumptionRecordCell.h" #import "KBConsumptionRecord.h" #import #import "UIColor+Extension.h" @interface KBConsumptionRecordCell () @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *timeLabel; @property (nonatomic, strong) UILabel *amountLabel; @property (nonatomic, strong) UIView *lineView; @end @implementation KBConsumptionRecordCell - (void)setupUI { self.contentView.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.titleLabel]; [self.contentView addSubview:self.timeLabel]; [self.contentView addSubview:self.amountLabel]; [self.contentView addSubview:self.lineView]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(16); make.top.equalTo(self.contentView).offset(14); make.right.lessThanOrEqualTo(self.amountLabel.mas_left).offset(-12); }]; [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.titleLabel); make.top.equalTo(self.titleLabel.mas_bottom).offset(6); make.bottom.equalTo(self.contentView).offset(-14); make.right.lessThanOrEqualTo(self.amountLabel.mas_left).offset(-12); }]; [self.amountLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView).offset(-16); make.centerY.equalTo(self.titleLabel); }]; [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(16); make.right.equalTo(self.contentView).offset(-16); make.bottom.equalTo(self.contentView); make.height.mas_equalTo(KB_ONE_PIXEL); }]; } - (void)configWithRecord:(KBConsumptionRecord *)record { NSString *title = record.kbdescription.length ? record.kbdescription : KBLocalized(@"Consumption"); self.titleLabel.text = title; self.timeLabel.text = record.createdAt.length ? record.createdAt : @"--"; NSString *displayAmount = [self kb_formatAmountText:record.amount]; self.amountLabel.text = displayAmount; UIColor *incomeColor = [UIColor colorWithHex:0x66CD7C]; UIColor *expenseColor = [UIColor colorWithHex:0xCD2853]; self.amountLabel.textColor = (record.type == KBConsumptionRecordTypeRecharge) ? incomeColor : expenseColor; } - (NSString *)kb_formatAmountText:(NSNumber *)amount { if (![amount isKindOfClass:NSNumber.class]) { return @"--"; } static NSNumberFormatter *formatter; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ formatter = [[NSNumberFormatter alloc] init]; formatter.numberStyle = NSNumberFormatterDecimalStyle; formatter.minimumFractionDigits = 2; formatter.maximumFractionDigits = 2; formatter.minimumIntegerDigits = 1; formatter.positivePrefix = @"+"; }); return [formatter stringFromNumber:amount] ?: @"--"; } #pragma mark - Lazy - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [UILabel new]; _titleLabel.font = [KBFont medium:15]; _titleLabel.textColor = [UIColor colorWithHex:KBBlackValue]; _titleLabel.text = KBLocalized(@"Consumption"); } return _titleLabel; } - (UILabel *)timeLabel { if (!_timeLabel) { _timeLabel = [UILabel new]; _timeLabel.font = [KBFont regular:12]; _timeLabel.textColor = [UIColor colorWithHex:0x9FA5B5]; _timeLabel.text = @"--"; } return _timeLabel; } - (UILabel *)amountLabel { if (!_amountLabel) { _amountLabel = [UILabel new]; _amountLabel.font = [KBFont medium:16]; _amountLabel.textColor = [UIColor colorWithHex:0xE36464]; _amountLabel.textAlignment = NSTextAlignmentRight; _amountLabel.text = @"--"; } return _amountLabel; } - (UIView *)lineView { if (!_lineView) { _lineView = [UIView new]; _lineView.backgroundColor = [UIColor colorWithHex:0xEFEFF1]; } return _lineView; } @end