QSLVipGoodCollectionViewCell.swift 12 KB

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