根据当前时间更改标签文本和颜色

Changing Label Text & Colour Depending On Current Time

我正在尝试更改您在我的屏幕截图中看到的标签,该标签具有绿色背景并显示我们开放。

我希望底部标签在列出的开放时间过后变为红色并显示 "Sorry we are closed",然后在正确的开放时间返回绿色并显示 "We Are Open"。

我已经成功地将日期和时间导入到顶部标签中,但我不确定底部标签是如何导入的。

代码如下:

import UIKit
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

class FirstViewController: UIViewController {

    var timer = Timer()

    @IBOutlet weak var timeLabel: UILabel!

    @IBOutlet weak var openStatusLabel: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        FIRMessaging.messaging().subscribe(toTopic: "/topics/news")

        self.timer = Timer.scheduledTimer(timeInterval: 1.0,
                                                            target: self,
                                                            selector: #selector(FirstViewController.tick),
                                                            userInfo: nil,
                                                            repeats: true)
    }

    @objc func tick() {
        timeLabel.text = DateFormatter.localizedString(from: NSDate() as Date,
                                                               dateStyle: .medium,
                                                               timeStyle: .medium)
    }

}

下面的代码我试过了,它会工作正常。最初我创建了两个具有适当约束的 UILabel 。然后为两个标签创建出口以查看控制器。然后试试这个代码。

import UIKit

extension NSDate {
    func dayOfWeek() -> Int? {
        guard
            let calender: NSCalendar = NSCalendar.currentCalendar(),
            let component: NSDateComponents = calender.components(.Weekday, fromDate: self) else { return nil }
        return component.weekday
    }
}

class ViewController: UIViewController {


    @IBOutlet var timeOfTheDay: UILabel! //Top Label for showing current time

    @IBOutlet var Status: UILabel!  //Status Label for showing open or close

    override func viewDidLoad() {
        super.viewDidLoad()

        self.dateCheck()



    }

    func dateCheck()
    {

        let today = NSDate().dayOfWeek()

        if today == 1
        {
            //print("Sunday")
            self.closed()

        }
        else if today == 2
        {
            //print("Monday")
            self.closed()
        }
        else if today == 3
        {
            //print("Tuesday")
            self.uptoEvening()
        }
        else if today == 4
        {
            //print("Wednesday")
            self.uptoNight()
        }
        else if today == 5
        {
          //  print("Thursday")
            self.uptoNight()

        }
        else if today == 6
        {
            //print("Friday")
            self.uptoNight()

        }
        else
        {
            //print("Saturday")
            self.uptoEvening()
        }
    }

    func getTime() -> (hour:Int, minute:Int, second:Int) {
        let currentDateTime = NSDate()
        let calendar = NSCalendar.currentCalendar()
        let component = calendar.components([.Hour,.Minute,.Second], fromDate: currentDateTime)
        let hour = component.hour
        let minute = component.minute
        let second = component.second
        return (hour,minute,second)
    }


    func closed()
    {
        timeOfTheDay.text = String(getTime().hour)+" : "+String(getTime().minute)+" : "+String(getTime().second)
        timeOfTheDay.backgroundColor = UIColor.redColor()
        timeOfTheDay.textColor = UIColor.whiteColor()

        Status.text = "Sorry! Today, We are Closed!"
        Status.backgroundColor = UIColor.redColor()
        Status.textColor = UIColor.whiteColor()

    }

    func opened(endTime:String)
    {
        timeOfTheDay.text = String(getTime().hour)+" : "+String(getTime().minute)+" : "+String(getTime().second)
        timeOfTheDay.backgroundColor = UIColor.greenColor()
        timeOfTheDay.textColor = UIColor.whiteColor()

        Status.text = "Hi! still we are opened upto "+endTime
        Status.backgroundColor = UIColor.greenColor()
        Status.textColor = UIColor.whiteColor()
    }


    func uptoEvening()
    {

        let time = getTime().hour

        switch time
        {
            case 09...16: opened("17")  //set time for 09:00 to 16:59
            default:closed()
        }
    }

    func uptoNight()
    {

        let time = getTime().hour

        switch time
        {
        case 09...20: opened("21") //set time for 09:00 to 20:59
        default:closed()
        }

    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }


}

swift3 的扩展名:

extension Date {
    func dayOfWeek() -> Int? {
            let calender: Calendar = Calendar.current
            let component: DateComponents = (calender as NSCalendar).components(.weekday, from: self)
        return component.weekday
    }
}