逆时标签
Reverse Time Label
我正在寻找可以为我提供反向计时器倒计时功能的标签。我知道有 Timer 可用于倒计时,但我想要像下图一样使用天、小时、分钟、秒倒计时。
谁能告诉我如何像下图那样实现它?
我们将不胜感激。谢谢。
首先你需要创建一个 NSAttributedString
与你的时间格式要求类似的东西
func timeLeftExtended(date:Date) ->NSAttributedString{
let cal = Calendar.current
let now = Date()
let calendarUnits:NSCalendar.Unit = [NSCalendar.Unit.day, NSCalendar.Unit.hour, NSCalendar.Unit.minute, NSCalendar.Unit.second]
let components = (cal as NSCalendar).components(calendarUnits, from: now, to: date, options: [])
let fullCountDownStr = "\(components.day!.description)d " + "\(components.hour!.description)h " + "\(components.minute!.description)m " + "\(components.second!.description)s "
let mutableStr = NSMutableAttributedString(string: fullCountDownStr, attributes: [NSAttributedStringKey.foregroundColor:UIColor.white])
for (index,char) in mutableStr.string.enumerated()
{
if(char == "d" || char == "h" || char == "m" || char == "s")
{
mutableStr.removeAttribute(NSAttributedStringKey.foregroundColor, range: NSMakeRange(index, 1))
mutableStr.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.lightGray], range: NSMakeRange(index, 1))
}
}
return mutableStr
}
之后您需要声明要更新剩余时间的标签
@IBOutlet weak var lblTimeRemaining: UILabel!
并添加一个计时器和一个标志以了解您的计时器何时工作
fileprivate var timeWorking : Bool = false
var timer:Timer?
在这里我们设置我们的定时器
func setupWith()
{
if(!timeWorking)
{
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.updateCountDown), userInfo: nil, repeats: true)
self.timeWorking = true
}
}
这个方法每秒会执行1次来更新我们的计数
@objc func updateCountDown()
{
self.lblTimeRemaining.attributedText = self.timeLeftExtended(date:Date.distantFuture)
}
结果
我正在寻找可以为我提供反向计时器倒计时功能的标签。我知道有 Timer 可用于倒计时,但我想要像下图一样使用天、小时、分钟、秒倒计时。
谁能告诉我如何像下图那样实现它?
我们将不胜感激。谢谢。
首先你需要创建一个 NSAttributedString
与你的时间格式要求类似的东西
func timeLeftExtended(date:Date) ->NSAttributedString{
let cal = Calendar.current
let now = Date()
let calendarUnits:NSCalendar.Unit = [NSCalendar.Unit.day, NSCalendar.Unit.hour, NSCalendar.Unit.minute, NSCalendar.Unit.second]
let components = (cal as NSCalendar).components(calendarUnits, from: now, to: date, options: [])
let fullCountDownStr = "\(components.day!.description)d " + "\(components.hour!.description)h " + "\(components.minute!.description)m " + "\(components.second!.description)s "
let mutableStr = NSMutableAttributedString(string: fullCountDownStr, attributes: [NSAttributedStringKey.foregroundColor:UIColor.white])
for (index,char) in mutableStr.string.enumerated()
{
if(char == "d" || char == "h" || char == "m" || char == "s")
{
mutableStr.removeAttribute(NSAttributedStringKey.foregroundColor, range: NSMakeRange(index, 1))
mutableStr.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.lightGray], range: NSMakeRange(index, 1))
}
}
return mutableStr
}
之后您需要声明要更新剩余时间的标签
@IBOutlet weak var lblTimeRemaining: UILabel!
并添加一个计时器和一个标志以了解您的计时器何时工作
fileprivate var timeWorking : Bool = false
var timer:Timer?
在这里我们设置我们的定时器
func setupWith()
{
if(!timeWorking)
{
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.updateCountDown), userInfo: nil, repeats: true)
self.timeWorking = true
}
}
这个方法每秒会执行1次来更新我们的计数
@objc func updateCountDown()
{
self.lblTimeRemaining.attributedText = self.timeLeftExtended(date:Date.distantFuture)
}
结果