ASProductItemView.m 6.6 KB

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