ASProductItemView.m 5.3 KB

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