QSLCountdownView.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // CountDownView.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2025/9/24.
  6. //
  7. import UIKit
  8. import YYText
  9. class QSLCountdownView: UIView {
  10. private let label = YYLabel()
  11. private var countdownType: Int // 1 :首页优惠券 2:引导页
  12. // 自定义初始化方法,传入Int参数
  13. init(frame: CGRect, type: Int) {
  14. self.countdownType = type // 存储传入的type
  15. super.init(frame: frame)
  16. setupUI()
  17. }
  18. required init?(coder: NSCoder) {
  19. fatalError("init(coder:) has not been implemented")
  20. }
  21. private func setupUI() {
  22. addSubview(label)
  23. label.snp.makeConstraints { make in
  24. make.edges.equalToSuperview()
  25. }
  26. }
  27. func updateCountdownText(_ timeString: String) {
  28. if (self.countdownType == 1){
  29. let fullText = " \(timeString) 限时特惠"
  30. let attributedText = NSMutableAttributedString(string: fullText)
  31. // 设置整体样式
  32. attributedText.yy_font = UIFont.textM(10)
  33. attributedText.yy_color = UIColor.white
  34. // 查找时间部分的range
  35. if let timeRange = fullText.range(of: timeString) {
  36. let nsRange = NSRange(timeRange, in: fullText)
  37. let timeAttributedString = NSMutableAttributedString(string: String(fullText[timeRange]))
  38. // 设置数字背景
  39. let bgColor = UIColor.white
  40. let bgBorder = YYTextBorder(fill: bgColor, cornerRadius: 2)
  41. bgBorder.insets = UIEdgeInsets(top: -3, left: -2.5, bottom: -2, right: -2)
  42. let bgBorder1 = YYTextBorder(fill: UIColor.clear, cornerRadius: 0)
  43. bgBorder1.insets = UIEdgeInsets(top: -3, left: -2, bottom: -2, right: -2)
  44. // 遍历每个字符
  45. for (index, char) in timeAttributedString.string.enumerated() {
  46. let range = NSRange(location: index, length: 1)
  47. if char.isNumber {
  48. timeAttributedString.yy_setTextBackgroundBorder(bgBorder, range: range)
  49. timeAttributedString.yy_setColor(UIColor.hexStringColor(hexString: "#0E5662"), range: range)
  50. } else {
  51. timeAttributedString.yy_setTextBackgroundBorder(bgBorder1, range: range)
  52. timeAttributedString.yy_setColor(bgColor, range: range)
  53. timeAttributedString.yy_setKern(NSNumber(value: 1), range: range)
  54. }
  55. }
  56. // 替换原始文本中的时间部分
  57. attributedText.replaceCharacters(in: nsRange, with: timeAttributedString)
  58. }
  59. label.attributedText = attributedText
  60. }else{
  61. let fullText = "优惠活动倒计时 \(timeString) 限时特惠"
  62. let attributedText = NSMutableAttributedString(string: fullText)
  63. // 设置整体样式
  64. attributedText.yy_font = UIFont.systemFont(ofSize: 11)
  65. attributedText.yy_color = UIColor.hexStringColor(hexString: "#FF5656")
  66. // 查找时间部分的range
  67. if let timeRange = fullText.range(of: timeString) {
  68. let nsRange = NSRange(timeRange, in: fullText)
  69. let timeAttributedString = NSMutableAttributedString(string: String(fullText[timeRange]))
  70. // 设置数字背景
  71. let bgColor = UIColor.hexStringColor(hexString: "#FF5656")
  72. let bgBorder = YYTextBorder(fill: bgColor, cornerRadius: 2)
  73. bgBorder.insets = UIEdgeInsets(top: -3, left: -2.5, bottom: -2, right: -2)
  74. let bgBorder1 = YYTextBorder(fill: UIColor.clear, cornerRadius: 0)
  75. bgBorder1.insets = UIEdgeInsets(top: -3, left: -2, bottom: -2, right: -2)
  76. // 遍历每个字符
  77. for (index, char) in timeAttributedString.string.enumerated() {
  78. let range = NSRange(location: index, length: 1)
  79. if char.isNumber {
  80. timeAttributedString.yy_setTextBackgroundBorder(bgBorder, range: range)
  81. timeAttributedString.yy_setColor(UIColor.white, range: range)
  82. } else {
  83. timeAttributedString.yy_setTextBackgroundBorder(bgBorder1, range: range)
  84. timeAttributedString.yy_setColor(bgColor, range: range)
  85. timeAttributedString.yy_setKern(NSNumber(value: 1), range: range)
  86. }
  87. }
  88. // 替换原始文本中的时间部分
  89. attributedText.replaceCharacters(in: nsRange, with: timeAttributedString)
  90. }
  91. label.attributedText = attributedText
  92. label.textAlignment = .center
  93. }
  94. }
  95. }