QSLVipGoodCollectionViewCell.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // QSLVipGoodCollectionViewCell.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/11/28.
  6. //
  7. import UIKit
  8. class QSLVipGoodCollectionViewCell: UICollectionViewCell {
  9. var goodModel: QSLGoodModel?
  10. lazy var bgView: UIView = {
  11. let view = UIView()
  12. view.backgroundColor = .white
  13. view.addRadius(radius: 8.rpx)
  14. view.addBorder(borderWidth: 2.rpx, borderColor: .hexStringColor(hexString: "#EEEEEE"))
  15. return view
  16. }()
  17. lazy var tagIcon: UIImageView = {
  18. let imageView = UIImageView()
  19. imageView.image = UIImage(named: "vip_goods_tag_icon")
  20. return imageView
  21. }()
  22. lazy var goodNameLabel: UILabel = {
  23. let label = UILabel()
  24. label.text("年度会员")
  25. label.boldFont(13)
  26. label.textColor = .hexStringColor(hexString: "#404040")
  27. return label
  28. }()
  29. lazy var goodDailyPriceLabel: UILabel = {
  30. let label = UILabel()
  31. let text = "0.35元/天"
  32. label.text(text)
  33. label.font(13)
  34. label.textColor = .hexStringColor(hexString: "#F12A1D")
  35. if let range = text.range(of: "/") {
  36. let nsRange = NSRange(text.startIndex..<range.lowerBound, in: text)
  37. label.setRangeFontText(font: .textM(20), range: nsRange)
  38. }
  39. return label
  40. }()
  41. lazy var priceLabel: UILabel = {
  42. let label = UILabel()
  43. label.textAlignment = .center
  44. label.addRadius(radius: 12.rpx)
  45. label.backgroundColor = .hexStringColor(hexString: "#F6F6F6")
  46. label.text("¥128")
  47. label.font(12)
  48. label.textColor = .hexStringColor(hexString: "#404040")
  49. return label
  50. }()
  51. lazy var originPriceLabel: UILabel = {
  52. let label = UILabel()
  53. label.text("¥299")
  54. label.font(13)
  55. label.textColor = .hexStringColor(hexString: "#A7A7A7")
  56. label.centerLineText(lineValue: 1, underlineColor: .hexStringColor(hexString: "#A7A7A7"))
  57. return label
  58. }()
  59. override init(frame: CGRect) {
  60. super.init(frame: frame)
  61. initView()
  62. }
  63. required init?(coder: NSCoder) {
  64. fatalError("init(coder:) has not been implemented")
  65. }
  66. func config(model: QSLGoodModel) {
  67. self.goodModel = model
  68. if model.isSelect {
  69. self.bgView.layer.borderColor = UIColor.hexStringColor(hexString: "#E7B983").cgColor
  70. self.bgView.backgroundColor = .hexStringColor(hexString: "#FFF8EF")
  71. } else {
  72. self.bgView.layer.borderColor = UIColor.hexStringColor(hexString: "#EEEEEE").cgColor
  73. self.bgView.backgroundColor = .white
  74. }
  75. self.tagIcon.isHidden = !model.popular
  76. self.goodNameLabel.text = model.name
  77. self.goodDailyPriceLabel.text = model.content
  78. if let range = model.content.range(of: "/") {
  79. let nsRange = NSRange(model.content.startIndex..<range.lowerBound, in: model.content)
  80. self.goodDailyPriceLabel.setRangeFontText(font: .textM(20), range: nsRange)
  81. }
  82. var priceText = ""
  83. if model.amount.truncatingRemainder(dividingBy: 100) == 0 {
  84. priceText = "¥\(Int(model.amount / 100))"
  85. } else {
  86. priceText = String(format: "¥%.2lf", model.amount / 100 )
  87. }
  88. self.priceLabel.text = priceText
  89. let width = priceText.singleLineWidth(font: .textF(12)) + 28.rpx
  90. self.priceLabel.snp.updateConstraints { make in
  91. make.width.equalTo(width)
  92. }
  93. var orinalPriceText = ""
  94. if model.originalAmount.truncatingRemainder(dividingBy: 100) == 0 {
  95. orinalPriceText = "¥\(Int(model.originalAmount / 100))"
  96. } else {
  97. orinalPriceText = String(format: "¥%.2lf", model.originalAmount / 100 )
  98. }
  99. self.originPriceLabel.text = orinalPriceText
  100. }
  101. }
  102. extension QSLVipGoodCollectionViewCell {
  103. func initView() {
  104. self.contentView.addSubview(bgView)
  105. bgView.snp.makeConstraints { make in
  106. make.right.bottom.equalTo(0)
  107. make.left.equalTo(6.rpx)
  108. make.top.equalTo(11.rpx)
  109. }
  110. self.contentView.addSubview(tagIcon)
  111. tagIcon.snp.makeConstraints { make in
  112. make.left.equalTo(0)
  113. make.top.equalTo(0)
  114. make.size.equalTo(CGSize(width: 79.rpx, height: 29.rpx))
  115. }
  116. self.bgView.addSubview(goodNameLabel)
  117. goodNameLabel.snp.makeConstraints { make in
  118. make.centerX.equalToSuperview()
  119. make.top.equalTo(16.rpx)
  120. }
  121. self.bgView.addSubview(goodDailyPriceLabel)
  122. goodDailyPriceLabel.snp.makeConstraints { make in
  123. make.centerX.equalToSuperview()
  124. make.top.equalTo(43.rpx)
  125. }
  126. self.bgView.addSubview(priceLabel)
  127. let width = "¥128".singleLineWidth(font: .textF(12)) + 28.rpx
  128. priceLabel.snp.makeConstraints { make in
  129. make.width.equalTo(width)
  130. make.height.equalTo(21.rpx)
  131. make.bottom.equalTo(-26.rpx)
  132. make.centerX.equalToSuperview()
  133. }
  134. self.bgView.addSubview(originPriceLabel)
  135. originPriceLabel.snp.makeConstraints { make in
  136. make.centerX.equalToSuperview()
  137. make.bottom.equalTo(-6.rpx)
  138. }
  139. }
  140. }