// // KBVipSubscribeCell.m // keyBoard // // 中文注释:订阅选项样式,右侧有选中图标,使用 mas 布局 + 懒加载。 // #import "KBVipSubscribeCell.h" @interface KBVipSubscribeCell () @property (nonatomic, strong) UIView *cardView; // 白色卡片背景 @property (nonatomic, strong) UILabel *titleLabel; // “Monthly Subscription” @property (nonatomic, strong) UILabel *priceLabel; // “$4.49” @property (nonatomic, strong) UILabel *strikeLabel; // 删除线原价 @property (nonatomic, strong) UIButton *selectButton; // 右侧选择按钮 @end @implementation KBVipSubscribeCell - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.contentView.backgroundColor = [UIColor clearColor]; [self.contentView addSubview:self.cardView]; [self.cardView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.contentView); make.top.equalTo(self.contentView).inset(0); make.bottom.equalTo(self.contentView); }]; [self.cardView addSubview:self.titleLabel]; [self.cardView addSubview:self.priceLabel]; [self.cardView addSubview:self.strikeLabel]; [self.cardView addSubview:self.selectButton]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.cardView).offset(16); make.bottom.equalTo(self.cardView.mas_centerY).offset(-5); }]; [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.titleLabel); make.top.equalTo(self.cardView.mas_centerY).offset(3); }]; [self.strikeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.priceLabel.mas_right).offset(10); make.centerY.equalTo(self.priceLabel); }]; [self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.cardView); make.right.equalTo(self.cardView).offset(-16); make.width.height.mas_equalTo(28); }]; // 基于 CALayer 的边框(替代 borderLayer) self.cardView.layer.borderWidth = 1; self.cardView.layer.borderColor = [UIColor colorWithWhite:0.9 alpha:1.0].CGColor; } return self; } - (void)layoutSubviews { [super layoutSubviews]; } - (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 price:(NSString *)price strike:(nullable NSString *)strike { self.titleLabel.text = title.length ? title : @"Monthly Subscription"; self.priceLabel.text = price.length ? price : @"$4.49"; self.strikeLabel.hidden = (strike.length == 0); if (strike.length) { // 加删除线 NSDictionary *attr = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle), NSForegroundColorAttributeName: [UIColor colorWithWhite:0.7 alpha:1.0] }; self.strikeLabel.attributedText = [[NSAttributedString alloc] initWithString:strike attributes:attr]; } } - (void)applySelected:(BOOL)selected animated:(BOOL)animated { UIImage *img = [UIImage imageNamed:(selected ? @"pay_circle_sel" : @"pay_circle_normal")]; [self.selectButton setImage:img forState:UIControlStateNormal]; CGColorRef color = (selected ? [UIColor colorWithHex:KBColorValue].CGColor : [UIColor colorWithWhite:0.9 alpha:1.0].CGColor); void (^changes)(void) = ^{ self.cardView.layer.borderColor = color; // self.cardView.layer.shadowOpacity = selected ? 0.12 : 0.0; // self.cardView.layer.shadowColor = [UIColor colorWithHex:KBColorValue].CGColor; // self.cardView.layer.shadowRadius = selected ? 8 : 0; // self.cardView.layer.shadowOffset = CGSizeMake(0, selected ? 4 : 0); }; changes(); } #pragma mark - Lazy - (UIView *)cardView { if (!_cardView) { _cardView = [UIView new]; _cardView.backgroundColor = [UIColor whiteColor]; CGFloat radius = 10; _cardView.layer.cornerRadius = radius; _cardView.clipsToBounds = YES; } return _cardView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [UILabel new]; _titleLabel.text = @"Monthly Subscription"; _titleLabel.textColor = [UIColor colorWithHex:KBBlackValue]; _titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightSemibold]; } return _titleLabel; } - (UILabel *)priceLabel { if (!_priceLabel) { _priceLabel = [UILabel new]; _priceLabel.text = @"$4.49"; _priceLabel.textColor = [UIColor colorWithHex:KBBlackValue]; _priceLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightBold]; } return _priceLabel; } - (UILabel *)strikeLabel { if (!_strikeLabel) { _strikeLabel = [UILabel new]; _strikeLabel.text = @"$4.49"; _strikeLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightSemibold]; _strikeLabel.textColor = [UIColor colorWithWhite:0.7 alpha:1.0]; } return _strikeLabel; } - (UIButton *)selectButton { if (!_selectButton) { _selectButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_selectButton setImage:[UIImage imageNamed:@"pay_circle_normal"] forState:UIControlStateNormal]; _selectButton.userInteractionEnabled = NO; // 由外层控制选中 _selectButton.contentMode = UIViewContentModeCenter; } return _selectButton; } @end