// // ASProductItemView.m // Asteria // // Created by iOS on 2023/5/17. // #import "ASProductItemView.h" @interface ASProductItemView () @property (nonatomic, strong) UIImageView *imgV; @property (nonatomic, strong) UILabel *titleLb; @property (nonatomic, strong) UILabel *nowPriceLb; @property (nonatomic, strong) UILabel *oldPriceLb; @property (nonatomic,strong) UILabel *hotLb; @property (nonatomic,strong) UIStackView *lbStackV; @end @implementation ASProductItemView - (void)setModel:(ASProductBaseModel *)model { _model = model; [self.imgV sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"product_defualtImg"]]; self.titleLb.text = model.title; self.hotLb.text = [NSString stringWithFormat:@"SOLD %@", model.sale_num]; self.nowPriceLb.text = model.nowPrice; if ([model.oldPrice isEqualToString:@""] || [model.oldPrice isEqualToString:model.nowPrice]) { [self.oldPriceLb setHidden:true]; } else { [self.oldPriceLb setHidden:false]; NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",model.oldPrice]]; [attStr addAttributes:@{ NSFontAttributeName:[UIFont fontWithName:Rob_Regular size:12], NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle), NSStrikethroughColorAttributeName:Col_999, NSForegroundColorAttributeName:Col_999, } range:NSMakeRange(0, attStr.length)]; self.oldPriceLb.attributedText = attStr; } } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self configSubV]; self.clipsToBounds = true; } return self; } - (void) configSubV { self.layer.cornerRadius = 8; self.layer.masksToBounds = true; self.backgroundColor = _F5F5F5; [self addSubview:self.imgV]; [self addSubview:self.titleLb]; [self addSubview:self.hotLb]; [self addSubview: self.nowPriceLb]; [self addSubview: self.oldPriceLb]; [self addSubview:self.addCartBt]; [self.imgV mas_makeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(self); make.height.equalTo(self.mas_width); make.top.leading.trailing.equalTo(self); }]; [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.imgV.mas_bottom).offset(10); make.leading.equalTo(self).offset(10); make.trailing.equalTo(self).offset(-10); }]; [self.hotLb mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.titleLb); make.top.equalTo(self.titleLb.mas_bottom).offset(7); make.trailing.equalTo(self.titleLb); make.height.equalTo(@14); }]; [self.nowPriceLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; [self.nowPriceLb mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.titleLb); make.top.greaterThanOrEqualTo(self.hotLb.mas_bottom).offset(11); make.height.equalTo(@17); make.bottom.equalTo(self.mas_bottom).offset(-9); }]; [self.oldPriceLb mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.nowPriceLb); make.leading.equalTo(self.nowPriceLb.mas_trailing).offset(4); make.height.equalTo(@16); }]; [self.addCartBt mas_makeConstraints:^(MASConstraintMaker *make) { make.width.height.equalTo(@24); make.trailing.equalTo(self).offset(-10); make.bottom.equalTo(self).offset(-10); make.leading.greaterThanOrEqualTo(self.oldPriceLb.mas_trailing).offset(8); }]; } - (UIImageView *)imgV { if (!_imgV) { UIImageView *v = [[UIImageView alloc] init]; v.contentMode = UIViewContentModeScaleToFill; _imgV = v; } return _imgV; } - (UILabel *)titleLb { if (!_titleLb) { UILabel *lb = [[UILabel alloc] init]; lb.font = [UIFont fontWithName:Rob_Regular size:12]; lb.textColor = [UIColor blackColor]; lb.textAlignment = NSTextAlignmentLeft; lb.numberOfLines = 2; _titleLb = lb; } return _titleLb; } - (UILabel *)nowPriceLb { if (!_nowPriceLb) { UILabel *lb = [[UILabel alloc] init]; lb.font = [UIFont fontWithName:Rob_Bold size:14]; lb.textColor = [UIColor blackColor]; lb.textAlignment = NSTextAlignmentLeft; _nowPriceLb = lb; } return _nowPriceLb; } - (UILabel *)oldPriceLb { if (!_oldPriceLb) { UILabel *lb = [[UILabel alloc] init]; lb.font = [UIFont fontWithName:Rob_Regular size:12]; lb.textColor = Col_999; lb.textAlignment = NSTextAlignmentLeft; _oldPriceLb = lb; } return _oldPriceLb; } - (UILabel *)hotLb { if (!_hotLb) { UILabel *lb = [[UILabel alloc] init]; lb.font = [UIFont fontWithName:Rob_Regular size:12]; lb.textColor = Col_666; lb.textAlignment = NSTextAlignmentLeft; _hotLb = lb; } return _hotLb; } -(UIButton *)addCartBt { if (!_addCartBt) { UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom]; [bt setImage:[UIImage imageNamed:@"product_addCart"] forState:UIControlStateNormal]; [bt addTarget:self action:@selector(addCartBtAction) forControlEvents:UIControlEventTouchUpInside]; _addCartBt = bt; } return _addCartBt; } - (void)addCartBtAction { if (self.addCartBlock) { self.addCartBlock(self.model); } } @end