QSLVipGoodCollectionViewCell.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // QSLVipGoodCollectionViewCell.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/11/28.
  6. //
  7. import UIKit
  8. enum GoodCellType {
  9. case big
  10. case small
  11. case top
  12. }
  13. class QSLVipGoodCollectionViewCell: UICollectionViewCell {
  14. struct UX {
  15. static let smallMainViewWidth = (QSLConst.qsl_kScreenW - 42.0.rpx) / 3.0 - 9.0.rpx
  16. static let mainViewHeight = 94.0.rpx
  17. static let bigMainViewWidth = (QSLConst.qsl_kScreenW - 36.0.rpx) / 2.0 - 9.0.rpx
  18. }
  19. var type: GoodCellType = .small {
  20. didSet {
  21. self.updateUI()
  22. }
  23. }
  24. var goodModel: QSLGoodModel?
  25. lazy var bgView: UIView = {
  26. let view = UIView()
  27. view.backgroundColor = .hexStringColor(hexString: "#EEEEEE")
  28. view.addRadius(radius: 12)
  29. return view
  30. }()
  31. lazy var mostTagIcon: UIImageView = {
  32. let imageView = UIImageView()
  33. imageView.image = UIImage(named: "vip_goods_most_tag")
  34. return imageView
  35. }()
  36. lazy var tagIcon: UIImageView = {
  37. let imageView = UIImageView()
  38. imageView.image = UIImage(named: "vip_goods_tag_icon")
  39. return imageView
  40. }()
  41. lazy var mainView: UIView = {
  42. let view = UIView(frame: CGRect(x: 0, y: 0, width: UX.smallMainViewWidth, height: UX.mainViewHeight))
  43. view.addFourCorner(topLeft: 11, topRight: 11, bottomLeft: 0, bottomRight: 0)
  44. view.backgroundColor = .white
  45. return view
  46. }()
  47. lazy var goodNameLabel: UILabel = {
  48. let label = UILabel()
  49. label.text("年度会员")
  50. label.boldFont(13)
  51. label.textColor = .hexStringColor(hexString: "#404040")
  52. return label
  53. }()
  54. lazy var priceLabel: UILabel = {
  55. let label = UILabel()
  56. label.textAlignment = .center
  57. // label.addRadius(radius: 12.rpx)
  58. // label.backgroundColor = .hexStringColor(hexString: "#F6F6F6")
  59. label.text("¥128")
  60. label.mediumFont(24)
  61. label.textColor = .hexStringColor(hexString: "#404040")
  62. label.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#404040"), font: UIFont.systemFont(ofSize: 14, weight: .medium))
  63. return label
  64. }()
  65. lazy var originPriceLabel: UILabel = {
  66. let label = UILabel()
  67. label.text("¥299")
  68. label.font(13)
  69. label.textColor = .hexStringColor(hexString: "#A7A7A7")
  70. label.centerLineText(lineValue: 1, underlineColor: .hexStringColor(hexString: "#A7A7A7"))
  71. return label
  72. }()
  73. lazy var goodDailyPriceLabel: UILabel = {
  74. let label = UILabel()
  75. let text = "仅需¥9.2/天,时刻守护Ta的安全"
  76. label.text(text)
  77. label.font(9)
  78. label.textColor = .hexStringColor(hexString: "#818181")
  79. label.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#818181"), font: UIFont.systemFont(ofSize: 9, weight: .heavy))
  80. if let yuanIndex = text.firstIndex(of: "¥") {
  81. // 获取文本最后一位的index
  82. let lastIndex = text.index(before: text.endIndex)
  83. // 创建从¥后面一个字符到文本最后一位的range
  84. let contentRange = text.index(after: yuanIndex)..<lastIndex
  85. // 将Swift的Range转换为NSRange
  86. let nsRange = NSRange(
  87. contentRange,
  88. in: text
  89. )
  90. label.setRangeFontText(font: UIFont.systemFont(ofSize: 12, weight: .heavy), range: nsRange)
  91. }
  92. return label
  93. }()
  94. lazy var selectBtn: UIButton = {
  95. let btn = UIButton()
  96. btn.setBackgroundImage(UIImage(named: "vip_good_unselect_big"), for: .normal)
  97. btn.setBackgroundImage(UIImage(named: "vip_good_select_big"), for: .selected)
  98. return btn
  99. }()
  100. override init(frame: CGRect) {
  101. super.init(frame: frame)
  102. initView()
  103. }
  104. required init?(coder: NSCoder) {
  105. fatalError("init(coder:) has not been implemented")
  106. }
  107. func config(model: QSLGoodModel, type: GoodCellType) {
  108. self.goodModel = model
  109. self.type = type
  110. self.selectBtn.isSelected = model.isSelect
  111. let text = model.content
  112. self.goodDailyPriceLabel.text = text
  113. if model.isSelect {
  114. self.bgView.backgroundColor = .hexStringColor(hexString: "#15CBA1")
  115. self.goodDailyPriceLabel.textColor = .white
  116. self.goodDailyPriceLabel.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#FFFFFF"), font: UIFont.systemFont(ofSize: 9, weight: .heavy))
  117. if let yuanIndex = text.firstIndex(of: "¥") {
  118. // 获取文本最后一位的index
  119. let lastIndex = text.index(before: text.endIndex)
  120. // 创建从¥后面一个字符到文本最后一位的range
  121. let contentRange = text.index(after: yuanIndex)...lastIndex
  122. // 将Swift的Range转换为NSRange
  123. let nsRange = NSRange(
  124. contentRange,
  125. in: text
  126. )
  127. self.goodDailyPriceLabel.setRangeFontText(font: UIFont.systemFont(ofSize: 12, weight: .heavy), range: nsRange)
  128. }
  129. } else {
  130. self.bgView.backgroundColor = .hexStringColor(hexString: "#EEEEEE")
  131. self.goodDailyPriceLabel.textColor = .hexStringColor(hexString: "#818181")
  132. self.goodDailyPriceLabel.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#818181"), font: UIFont.systemFont(ofSize: 9, weight: .heavy))
  133. if let yuanIndex = text.firstIndex(of: "¥") {
  134. // 获取文本最后一位的index
  135. let lastIndex = text.index(before: text.endIndex)
  136. // 创建从¥后面一个字符到文本最后一位的range
  137. let contentRange = text.index(after: yuanIndex)...lastIndex
  138. // 将Swift的Range转换为NSRange
  139. let nsRange = NSRange(
  140. contentRange,
  141. in: text
  142. )
  143. self.goodDailyPriceLabel.setRangeFontText(font: UIFont.systemFont(ofSize: 12, weight: .heavy), range: nsRange)
  144. }
  145. }
  146. self.mostTagIcon.isHidden = !model.popular
  147. self.goodNameLabel.text = model.name
  148. // self.goodDailyPriceLabel.text = "0.001元/天"
  149. // let width = model.content.singleLineWidth(font: .textF(10)) + 24.rpx
  150. // self.goodDailyPriceLabel.snp.updateConstraints { make in
  151. // make.width.equalTo(width)
  152. // }
  153. // if let range = model.content.range(of: "/") {
  154. // let nsRange = NSRange(model.content.startIndex..<range.lowerBound, in: model.content)
  155. // self.goodDailyPriceLabel.setRangeFontText(font: .textM(20), range: nsRange)
  156. // }
  157. var priceText = ""
  158. if model.amount.truncatingRemainder(dividingBy: 100) == 0 {
  159. priceText = "¥\(Int(model.amount / 100))"
  160. } else {
  161. priceText = String(format: "¥%.2lf", model.amount / 100 )
  162. }
  163. self.priceLabel.text = priceText
  164. self.priceLabel.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#404040"), font: UIFont.systemFont(ofSize: 14, weight: .medium))
  165. // let width = priceText.singleLineWidth(font: .textF(12)) + 28.rpx
  166. // self.priceLabel.snp.updateConstraints { make in
  167. // make.width.equalTo(width)
  168. // }
  169. var orinalPriceText = ""
  170. if model.originalAmount.truncatingRemainder(dividingBy: 100) == 0 {
  171. orinalPriceText = "¥\(Int(model.originalAmount / 100))"
  172. } else {
  173. orinalPriceText = String(format: "¥%.2lf", model.originalAmount / 100 )
  174. }
  175. self.originPriceLabel.text = orinalPriceText
  176. }
  177. }
  178. extension QSLVipGoodCollectionViewCell {
  179. func updateUI() {
  180. if self.type == .big {
  181. self.mainView.frame = CGRect(x: 0, y: 0, width: UX.bigMainViewWidth, height: UX.mainViewHeight)
  182. goodNameLabel.snp.remakeConstraints { make in
  183. make.centerX.equalToSuperview()
  184. make.top.equalTo(15.rpx)
  185. }
  186. priceLabel.snp.remakeConstraints { make in
  187. make.centerX.equalToSuperview()
  188. make.top.equalTo(40.rpx)
  189. }
  190. originPriceLabel.snp.remakeConstraints { make in
  191. make.centerX.equalToSuperview()
  192. make.top.equalTo(priceLabel.snp.bottom).offset(0.rpx)
  193. }
  194. goodDailyPriceLabel.snp.remakeConstraints { make in
  195. make.centerX.equalToSuperview()
  196. make.centerY.equalTo(selectBtn.snp.centerY)
  197. }
  198. } else {
  199. self.mainView.frame = CGRect(x: 0, y: 0, width: UX.smallMainViewWidth, height: UX.mainViewHeight)
  200. }
  201. self.mainView.addFourCorner(topLeft: 11, topRight: 11, bottomLeft: 0, bottomRight: 0)
  202. }
  203. func initView() {
  204. self.contentView.addSubview(bgView)
  205. bgView.snp.makeConstraints { make in
  206. make.right.bottom.equalTo(0)
  207. make.left.equalTo(6.rpx)
  208. make.top.equalTo(11.rpx)
  209. }
  210. self.contentView.addSubview(mostTagIcon)
  211. mostTagIcon.snp.makeConstraints { make in
  212. make.left.equalTo(0)
  213. make.top.equalTo(0)
  214. make.size.equalTo(CGSize(width: 85.rpx, height: 29.rpx))
  215. }
  216. // self.addSubview(mostTagIcon)
  217. // mostTagIcon.snp.makeConstraints { make in
  218. // make.left.equalTo(0)
  219. // make.top.equalTo(0)
  220. // make.size.equalTo(CGSize(width: 85.rpx, height: 29.rpx))
  221. // }
  222. self.bgView.addSubview(mainView)
  223. mainView.snp.makeConstraints { make in
  224. make.top.equalTo(2.rpx)
  225. make.left.equalTo(2.rpx)
  226. make.right.equalTo(-2)
  227. make.height.equalTo(UX.mainViewHeight)
  228. }
  229. self.mainView.addSubview(goodNameLabel)
  230. goodNameLabel.snp.makeConstraints { make in
  231. make.left.equalTo(10.rpx)
  232. make.top.equalTo(15.rpx)
  233. }
  234. self.mainView.addSubview(priceLabel)
  235. priceLabel.snp.makeConstraints { make in
  236. make.top.equalTo(40.rpx)
  237. make.left.equalTo(10.rpx)
  238. }
  239. self.mainView.addSubview(originPriceLabel)
  240. originPriceLabel.snp.makeConstraints { make in
  241. make.left.equalTo(10.rpx)
  242. make.top.equalTo(priceLabel.snp.bottom).offset(0.rpx)
  243. }
  244. self.bgView.addSubview(selectBtn)
  245. selectBtn.snp.makeConstraints { make in
  246. make.size.equalTo(CGSize(width: 16.rpx, height: 16.rpx))
  247. make.right.equalTo(-6.rpx)
  248. make.bottom.equalTo(-6.rpx)
  249. }
  250. self.bgView.addSubview(goodDailyPriceLabel)
  251. goodDailyPriceLabel.snp.makeConstraints { make in
  252. make.left.equalTo(10.rpx)
  253. make.centerY.equalTo(selectBtn.snp.centerY)
  254. }
  255. // let width = "0.001元/天".singleLineWidth(font: .textF(10)) + 24.rpx
  256. // self.bgView.addSubview(goodDailyPriceLabel)
  257. // goodDailyPriceLabel.snp.makeConstraints { make in
  258. // make.centerX.equalToSuperview()
  259. // make.bottom.equalTo(-8.rpx)
  260. // make.height.equalTo(21.rpx)
  261. // make.width.equalTo(width)
  262. // }
  263. }
  264. }