QSLActivityVipCell.swift 13 KB

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