简体   繁体   English

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

[英]Changing Label Text & Colour Depending On Current Time

应用截图

I am trying to change the label that you see in my screenshot that has a green background and says We Are Open. 我正在尝试更改您在屏幕快照中看到的带有绿色背景的标签,并说我们在打开。

I would like the bottom label to turn RED and say "Sorry we are closed" whenever the listed opening times have passed and then go back to GREEN and say "We Are Open" at the correct opening times. 我希望底部标签在列出的开放时间过去后变成红色并说“对不起,我们已经关闭”,然后返回绿色,并在正确的开放时间说“我们开放了”。

I've managed to import date and time successfully into the top label but I'm not sure how do the bottom label. 我已成功将日期和时间成功导入顶部标签,但不确定底部标签如何。

Here is the code: 这是代码:

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)
    }

}

Below code i have try and it will work fine. 下面的代码我尝试过,它将正常工作。 Initially i created two UILabel with proper constraints. 最初,我创建了两个具有适当约束的UILabel Then Create outlets for two label to view controller. 然后为两个标签创建插座以查看控制器。 Then try this code. 然后尝试此代码。

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()

    }


}

Extension for swift 3: 快速扩展3:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM