ASProductItemView.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // ASProductItemView.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/5/17.
  6. //
  7. #import "ASProductItemView.h"
  8. @interface ASProductItemView ()
  9. @property (nonatomic, strong) UIImageView *imgV;
  10. @property (nonatomic, strong) UILabel *titleLb;
  11. @property (nonatomic, strong) UILabel *nowPriceLb;
  12. @property (nonatomic, strong) UILabel *oldPriceLb;
  13. @property (nonatomic,strong) UILabel *hotLb;
  14. @property (nonatomic,strong) UIStackView *lbStackV;
  15. @end
  16. @implementation ASProductItemView
  17. - (void)setModel:(ASProductBaseModel *)model {
  18. _model = model;
  19. [self.imgV sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"product_defualtImg"]];
  20. self.titleLb.text = model.title;
  21. self.hotLb.text = [NSString stringWithFormat:@"SOLD %@", model.sale_num];
  22. self.nowPriceLb.text = model.nowPrice;
  23. if ([model.oldPrice isEqualToString:@""] || [model.oldPrice isEqualToString:model.nowPrice]) {
  24. [self.oldPriceLb setHidden:true];
  25. } else {
  26. [self.oldPriceLb setHidden:false];
  27. NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",model.oldPrice]];
  28. [attStr addAttributes:@{
  29. NSFontAttributeName:[UIFont fontWithName:Rob_Regular size:12],
  30. NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),
  31. NSStrikethroughColorAttributeName:Col_999,
  32. NSForegroundColorAttributeName:Col_999,
  33. } range:NSMakeRange(0, attStr.length)];
  34. self.oldPriceLb.attributedText = attStr;
  35. }
  36. }
  37. - (instancetype)initWithFrame:(CGRect)frame {
  38. self = [super initWithFrame:frame];
  39. if (self) {
  40. [self configSubV];
  41. self.clipsToBounds = true;
  42. }
  43. return self;
  44. }
  45. - (void) configSubV {
  46. self.layer.cornerRadius = 8;
  47. self.layer.masksToBounds = true;
  48. self.backgroundColor = _F5F5F5;
  49. [self addSubview:self.imgV];
  50. [self addSubview:self.titleLb];
  51. [self addSubview:self.hotLb];
  52. [self addSubview: self.nowPriceLb];
  53. [self addSubview: self.oldPriceLb];
  54. [self addSubview:self.addCartBt];
  55. [self.imgV mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.width.equalTo(self);
  57. make.height.equalTo(self.mas_width);
  58. make.top.leading.trailing.equalTo(self);
  59. }];
  60. [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.top.equalTo(self.imgV.mas_bottom).offset(10);
  62. make.leading.equalTo(self).offset(10);
  63. make.trailing.equalTo(self).offset(-10);
  64. }];
  65. [self.hotLb mas_makeConstraints:^(MASConstraintMaker *make) {
  66. make.leading.equalTo(self.titleLb);
  67. make.top.equalTo(self.titleLb.mas_bottom).offset(7);
  68. make.trailing.equalTo(self.titleLb);
  69. make.height.equalTo(@14);
  70. }];
  71. [self.nowPriceLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
  72. [self.nowPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
  73. make.leading.equalTo(self.titleLb);
  74. make.top.greaterThanOrEqualTo(self.hotLb.mas_bottom).offset(11);
  75. make.height.equalTo(@17);
  76. make.bottom.equalTo(self.mas_bottom).offset(-9);
  77. }];
  78. [self.oldPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
  79. make.bottom.equalTo(self.nowPriceLb);
  80. make.leading.equalTo(self.nowPriceLb.mas_trailing).offset(4);
  81. make.height.equalTo(@16);
  82. }];
  83. [self.addCartBt mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.width.height.equalTo(@24);
  85. make.trailing.equalTo(self).offset(-10);
  86. make.bottom.equalTo(self).offset(-10);
  87. make.leading.greaterThanOrEqualTo(self.oldPriceLb.mas_trailing).offset(8);
  88. }];
  89. }
  90. - (UIImageView *)imgV {
  91. if (!_imgV) {
  92. UIImageView *v = [[UIImageView alloc] init];
  93. v.contentMode = UIViewContentModeScaleToFill;
  94. _imgV = v;
  95. }
  96. return _imgV;
  97. }
  98. - (UILabel *)titleLb {
  99. if (!_titleLb) {
  100. UILabel *lb = [[UILabel alloc] init];
  101. lb.font = [UIFont fontWithName:Rob_Regular size:12];
  102. lb.textColor = [UIColor blackColor];
  103. lb.textAlignment = NSTextAlignmentLeft;
  104. lb.numberOfLines = 2;
  105. _titleLb = lb;
  106. }
  107. return _titleLb;
  108. }
  109. - (UILabel *)nowPriceLb {
  110. if (!_nowPriceLb) {
  111. UILabel *lb = [[UILabel alloc] init];
  112. lb.font = [UIFont fontWithName:Rob_Bold size:14];
  113. lb.textColor = [UIColor blackColor];
  114. lb.textAlignment = NSTextAlignmentLeft;
  115. _nowPriceLb = lb;
  116. }
  117. return _nowPriceLb;
  118. }
  119. - (UILabel *)oldPriceLb {
  120. if (!_oldPriceLb) {
  121. UILabel *lb = [[UILabel alloc] init];
  122. lb.font = [UIFont fontWithName:Rob_Regular size:12];
  123. lb.textColor = Col_999;
  124. lb.textAlignment = NSTextAlignmentLeft;
  125. _oldPriceLb = lb;
  126. }
  127. return _oldPriceLb;
  128. }
  129. - (UILabel *)hotLb {
  130. if (!_hotLb) {
  131. UILabel *lb = [[UILabel alloc] init];
  132. lb.font = [UIFont fontWithName:Rob_Regular size:12];
  133. lb.textColor = Col_666;
  134. lb.textAlignment = NSTextAlignmentLeft;
  135. _hotLb = lb;
  136. }
  137. return _hotLb;
  138. }
  139. -(UIButton *)addCartBt {
  140. if (!_addCartBt) {
  141. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  142. [bt setImage:[UIImage imageNamed:@"product_addCart"] forState:UIControlStateNormal];
  143. [bt addTarget:self action:@selector(addCartBtAction) forControlEvents:UIControlEventTouchUpInside];
  144. _addCartBt = bt;
  145. }
  146. return _addCartBt;
  147. }
  148. - (void)addCartBtAction {
  149. if (self.addCartBlock) {
  150. self.addCartBlock(self.model);
  151. }
  152. }
  153. @end