// // KBSvipSubscribeCell.m // keyBoard // // SVIP 订阅选项样式,使用背景图片切换选中状态 // #import "KBSvipSubscribeCell.h" @interface KBSvipSubscribeCell () @property (nonatomic, strong) UIImageView *bgImageView; // 背景图片 @property (nonatomic, strong) UILabel *titleLabel; // "1 Week" / "1 Month" / "1 Year" @property (nonatomic, strong) UIView *priceContainer; // 白色圆角容器 @property (nonatomic, strong) UILabel *currencyLabel; // "$" 货币符号 @property (nonatomic, strong) UILabel *priceLabel; // "6.90" 价格数字 @property (nonatomic, strong) UILabel *strikeLabel; // 删除线原价 @end @implementation KBSvipSubscribeCell - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.contentView.backgroundColor = [UIColor clearColor]; [self.contentView addSubview:self.bgImageView]; [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.contentView); }]; [self.contentView addSubview:self.titleLabel]; [self.contentView addSubview:self.priceContainer]; // 价格容器内添加货币符号和价格 [self.priceContainer addSubview:self.currencyLabel]; [self.priceContainer addSubview:self.priceLabel]; [self.priceContainer addSubview:self.strikeLabel]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.contentView); make.top.equalTo(self.contentView).offset(10); }]; [self.priceContainer mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(5); make.right.equalTo(self.contentView).offset(-5); make.bottom.equalTo(self.contentView).offset(-5); make.height.mas_equalTo(86); }]; // 货币符号和价格水平居中 [self.currencyLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.priceLabel.mas_left).offset(-2); make.bottom.equalTo(self.priceLabel.mas_bottom).offset(-4); // 底部对齐,稍微上移 }]; [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.priceContainer).offset(6); // 稍微右移,给货币符号留空间 make.centerY.equalTo(self.priceContainer).offset(-8); }]; [self.strikeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.priceContainer); make.top.equalTo(self.priceLabel.mas_bottom).offset(4); }]; } return self; } - (void)prepareForReuse { [super prepareForReuse]; [self applySelected:NO animated:NO]; } - (void)setSelected:(BOOL)selected { [super setSelected:selected]; [self applySelected:selected animated:NO]; } - (void)configTitle:(NSString *)title currency:(NSString *)currency price:(NSString *)price strike:(nullable NSString *)strike { self.titleLabel.text = title.length ? title : @"1 Month"; self.currencyLabel.text = currency.length ? currency : @"$"; self.priceLabel.text = price.length ? price : @"6.90"; self.strikeLabel.hidden = (strike.length == 0); if (strike.length) { NSDictionary *attr = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle), NSForegroundColorAttributeName: [UIColor colorWithHex:0x999999] }; self.strikeLabel.attributedText = [[NSAttributedString alloc] initWithString:strike attributes:attr]; } } // 兼容旧方法 - (void)configTitle:(NSString *)title price:(NSString *)price strike:(nullable NSString *)strike { [self configTitle:title currency:@"$" price:price strike:strike]; } - (void)applySelected:(BOOL)selected animated:(BOOL)animated { NSString *imageName = selected ? @"pay_colorbg_icon" : @"pay_graybg_icon"; void (^changes)(void) = ^{ self.bgImageView.image = [UIImage imageNamed:imageName]; }; if (animated) { [UIView transitionWithView:self.bgImageView duration:0.2 options:UIViewAnimationOptionTransitionCrossDissolve animations:changes completion:nil]; } else { changes(); } } #pragma mark - Lazy - (UIImageView *)bgImageView { if (!_bgImageView) { _bgImageView = [UIImageView new]; _bgImageView.contentMode = UIViewContentModeScaleAspectFill; _bgImageView.image = [UIImage imageNamed:@"pay_graybg_icon"]; } return _bgImageView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [UILabel new]; _titleLabel.text = @"1 Month"; _titleLabel.textColor = [UIColor colorWithHex:KBBlackValue]; _titleLabel.font = [KBFont medium:13]; } return _titleLabel; } - (UIView *)priceContainer { if (!_priceContainer) { _priceContainer = [UIView new]; _priceContainer.backgroundColor = [UIColor whiteColor]; _priceContainer.layer.cornerRadius = 14; _priceContainer.clipsToBounds = YES; } return _priceContainer; } - (UILabel *)currencyLabel { if (!_currencyLabel) { _currencyLabel = [UILabel new]; _currencyLabel.text = @"$"; _currencyLabel.textColor = [UIColor colorWithHex:KBBlackValue]; _currencyLabel.font = [KBFont medium:14]; // 货币符号字体较小 } return _currencyLabel; } - (UILabel *)priceLabel { if (!_priceLabel) { _priceLabel = [UILabel new]; _priceLabel.text = @"6.90"; _priceLabel.textColor = [UIColor colorWithHex:KBBlackValue]; _priceLabel.font = [KBFont bold:22]; // 价格数字字体较大 } return _priceLabel; } - (UILabel *)strikeLabel { if (!_strikeLabel) { _strikeLabel = [UILabel new]; _strikeLabel.text = @"$4.49"; _strikeLabel.font = [KBFont regular:12]; _strikeLabel.textColor = [UIColor colorWithHex:0x999999]; } return _strikeLabel; } @end