简体   繁体   English

Swift 3-要标记的DateComponents。文本有时显示“…”

[英]Swift 3 - DateComponents to Label.Text sometimes displays “…”

This is driving me insane. 这让我发疯。

I am trying to find the difference in two times the current time and a set time which will eventually be grabbed from the server. 我试图找到当前时间和设定时间的两倍,最终将两者从服务器中获取。

This is the time getTimeDifference() function for the moment it has no parameters. 这是现在没有参数的时间getTimeDifference()函数。

let formatter = DateFormatter()
let userCalender = Calendar.current;
let requestedComponent : Set<Calendar.Component> = [.month,.day,.hour,.minute,.second]


public func getTimeDifference() -> DateComponents
{
    formatter.dateFormat = "MM/dd/yyyy hh:mm:ss a"

    let _startTime = Date()
    let _endTime = formatter.date(from: "12/05/2016 14:01:00 a")


    let timeDifference = userCalender.dateComponents(requestedComponent, from: _startTime, to: _endTime!)
    return timeDifference
}

This is what I have in my HomepageViewController 这就是我的HomepageViewController中的内容

@IBOutlet weak var labelHours: UILabel!
@IBOutlet weak var labelMinutes: UILabel!
@IBOutlet weak var labelSeconds: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCountdown), userInfo: nil, repeats: true).fire()
    // Do any additional setup after loading the view.
}

func updateCountdown() -> Void {

    let time = Countdown().getTimeDifference()

    labelHours.text = time.hour?.description
    labelMinutes.text = time.minute?.description
    labelSeconds.text = time.second?.description
}

When ever I run this code it may work completely fine for a few seconds however some of the intervals it will display ... 每当我运行此代码时,它可能会在几秒钟内完全正常运行,但是它会显示一些间隔...

工作 不工作

The question mark (?) produces the images above and removing the (?) sets all the labels as ... 问号(?)产生上面的图像,而删除(?)会将所有标签设置为...

labelHours.text = time.hour?.description
labelMinutes.text = time.minute?.description
labelSeconds.text = time.second?.description

labelHours.text = time.hour.description
labelMinutes.text = time.minute.description
labelSeconds.text = time.second.description

Makes the frame fit the size of the text. 使框架适合文本的大小。 What you are looking for is: 您正在寻找的是:

questionsLabel.adjustsFontSizeToFitWidth = true

Above code will change the font size to fit the width of label. 上面的代码将更改字体大小以适合标签的宽度。 and also add 并添加

questionsLabel.textAlignment = .center

It will render text properly in label. 它将在标签中正确显示文本。

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

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