// // ASProductItemView.m // Asteria // // Created by iOS on 2023/5/17. // #import "ASProductItemView.h" @interface ASProductItemView () @property (nonatomic, strong) UIImageView *imgV; @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]; self.nowPriceLb.text = model.nowPrice; } else { // [self.oldPriceLb setHidden:false]; CGRect rect1 = [model.nowPrice boundingRectWithSize:CGSizeMake(MAXFLOAT, 22) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:Rob_Bold size:14]} context:nil]; CGRect rect2 = [model.oldPrice boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:Rob_Regular size:12]} context:nil]; NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@", model.nowPrice, model.oldPrice]]; if (rect1.size.width + rect2.size.width + 5 > (KScreenWidth - 30)/2 - 20 - 24) { attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@", model.nowPrice, model.oldPrice]]; } [attStr addAttributes:@{ NSFontAttributeName:[UIFont fontWithName:Rob_Regular size:12], NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle), NSStrikethroughColorAttributeName:Col_999, NSForegroundColorAttributeName:Col_999, } range:NSMakeRange(model.nowPrice.length + 1, model.oldPrice.length)]; // CGRect rect = [attStr boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil]; self.nowPriceLb.attributedText = attStr; // self.nowPriceLb.text = model.nowPrice; } } - (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.equalTo(self.titleLb.mas_bottom).offset(4); make.height.mas_equalTo(@34); make.right.mas_equalTo(-36); make.bottom.equalTo(self.mas_bottom).offset(-6); }]; // [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.adjustsFontSizeToFitWidth = YES; lb.numberOfLines = 2; 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