// // CountDownView.swift // QuickSearchLocation // // Created by Destiny on 2025/9/24. // import UIKit import YYText class QSLCountdownView: UIView { let label = YYLabel() private var countdownType: Int // 1 :首页优惠券 2:引导页 // 自定义初始化方法,传入Int参数 init(frame: CGRect, type: Int) { self.countdownType = type // 存储传入的type super.init(frame: frame) setupUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setupUI() { addSubview(label) label.snp.makeConstraints { make in make.edges.equalToSuperview() } } func updateCountdownText(_ timeString: String) { if (self.countdownType == 1){ let fullText = " \(timeString) 限时特惠" let attributedText = NSMutableAttributedString(string: fullText) // 设置整体样式 attributedText.yy_font = UIFont.textM(10) attributedText.yy_color = UIColor.white // 查找时间部分的range if let timeRange = fullText.range(of: timeString) { let nsRange = NSRange(timeRange, in: fullText) let timeAttributedString = NSMutableAttributedString(string: String(fullText[timeRange])) // 设置数字背景 let bgColor = UIColor.white let bgBorder = YYTextBorder(fill: bgColor, cornerRadius: 2) bgBorder.insets = UIEdgeInsets(top: -3, left: -3.5, bottom: -2, right: -3) let bgBorder1 = YYTextBorder(fill: UIColor.clear, cornerRadius: 0) bgBorder1.insets = UIEdgeInsets(top: -3, left: -3, bottom: -2, right: -3) // 遍历每个字符 for (index, char) in timeAttributedString.string.enumerated() { let range = NSRange(location: index, length: 1) if char.isNumber { timeAttributedString.yy_setTextBackgroundBorder(bgBorder, range: range) timeAttributedString.yy_setColor(UIColor.hexStringColor(hexString: "#0E5662"), range: range) } else { timeAttributedString.yy_setTextBackgroundBorder(bgBorder1, range: range) timeAttributedString.yy_setColor(bgColor, range: range) timeAttributedString.yy_setKern(NSNumber(value: 1), range: range) } } // 替换原始文本中的时间部分 attributedText.replaceCharacters(in: nsRange, with: timeAttributedString) } label.attributedText = attributedText }else{ let fullText = "优惠活动倒计时 \(timeString) 限时特惠" let attributedText = NSMutableAttributedString(string: fullText) // 设置整体样式 attributedText.yy_font = UIFont.systemFont(ofSize: 11) attributedText.yy_color = UIColor.hexStringColor(hexString: "#FF5656") // 查找时间部分的range if let timeRange = fullText.range(of: timeString) { let nsRange = NSRange(timeRange, in: fullText) let timeAttributedString = NSMutableAttributedString(string: String(fullText[timeRange])) // 设置数字背景 let bgColor = UIColor.hexStringColor(hexString: "#FF5656") let bgBorder = YYTextBorder(fill: bgColor, cornerRadius: 2) bgBorder.insets = UIEdgeInsets(top: -3, left: -2.5, bottom: -2, right: -2) let bgBorder1 = YYTextBorder(fill: UIColor.clear, cornerRadius: 0) bgBorder1.insets = UIEdgeInsets(top: -3, left: -2, bottom: -2, right: -2) // 遍历每个字符 for (index, char) in timeAttributedString.string.enumerated() { let range = NSRange(location: index, length: 1) if char.isNumber { timeAttributedString.yy_setTextBackgroundBorder(bgBorder, range: range) timeAttributedString.yy_setColor(UIColor.white, range: range) } else { timeAttributedString.yy_setTextBackgroundBorder(bgBorder1, range: range) timeAttributedString.yy_setColor(bgColor, range: range) timeAttributedString.yy_setKern(NSNumber(value: 1), range: range) } } // 替换原始文本中的时间部分 attributedText.replaceCharacters(in: nsRange, with: timeAttributedString) } label.attributedText = attributedText label.textAlignment = .center } } }