| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- //
- // CountDownView.swift
- // QuickSearchLocation
- //
- // Created by Destiny on 2025/9/24.
- //
- import UIKit
- import YYText
- class QSLCountdownView: UIView {
- private 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: -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.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
- }
- }
- }
|