我顯示倒計時計時器使用一個UILabel這樣的:的UILabel顯示倒計時文本
func updateTime() {
var elapsedTime: NSTimeInterval = startDate!.timeIntervalSinceNow
var daysFloat = floor(elapsedTime/24/60/60)
var hoursLeftFloat = floor((elapsedTime) - (daysFloat*86400))
var hoursFloat = floor(hoursLeftFloat/3600)
var minutesLeftFloat = floor((hoursLeftFloat) - (hoursFloat*3600))
var minutesFloat = floor(minutesLeftFloat/60)
var remainingSeconds = elapsedTime % 60
var daysInt = Int(daysFloat)
var hoursInt = Int(hoursFloat)
var minutesInt = Int(minutesFloat)
var remainingSecondsInt = Int(remainingSeconds)
var startsIn = NSMutableAttributedString()
var days = NSMutableAttributedString()
var hours = NSMutableAttributedString()
var minutes = NSMutableAttributedString()
var seconds = NSMutableAttributedString()
var dot = NSMutableAttributedString()
startsIn = NSMutableAttributedString(string: "STARTS IN: ", attributes: [NSFontAttributeName:UIFont(name: "Tungsten-Semibold", size: 37.0)!, NSForegroundColorAttributeName:color])
days = NSMutableAttributedString(string: String(format: "%02d", daysInt) + " DAYS", attributes: [NSFontAttributeName:UIFont(name: "Tungsten-Semibold", size: 37.0)!, NSForegroundColorAttributeName:UIColor.blackColor()])
hours = NSMutableAttributedString(string: String(format: "%02d", hoursInt) + " HRS", attributes: [NSFontAttributeName:UIFont(name: "Tungsten-Semibold", size: 37.0)!, NSForegroundColorAttributeName:UIColor.blackColor()])
minutes = NSMutableAttributedString(string: String(format: "%02d", minutesInt) + " MIN", attributes: [NSFontAttributeName:UIFont(name: "Tungsten-Semibold", size: 37.0)!, NSForegroundColorAttributeName:UIColor.blackColor()])
seconds = NSMutableAttributedString(string: String(format: "%02d", remainingSecondsInt) + " SEC", attributes: [NSFontAttributeName:UIFont(name: "Tungsten-Semibold", size: 37.0)!, NSForegroundColorAttributeName:UIColor.blackColor()])
dot = NSMutableAttributedString(string: " . ", attributes: [NSFontAttributeName:UIFont(name: "Tungsten-Semibold", size: 39.0)!, NSForegroundColorAttributeName:UIColor.lightGrayColor()])
var countdownText = NSMutableAttributedString()
countdownText.appendAttributedString(startsIn)
countdownText.appendAttributedString(days)
countdownText.appendAttributedString(dot)
countdownText.appendAttributedString(hours)
countdownText.appendAttributedString(dot)
countdownText.appendAttributedString(minutes)
countdownText.appendAttributedString(dot)
countdownText.appendAttributedString(seconds)
countdownLabel.attributedText = countdownText
}
我有我的UILabel啓用自動收縮,使該字體將縮小,右看看上的所有設備。我看到的問題是,某些秒和/或分鐘會導致字體大小跳躍,並且它會看起來很尷尬,標籤文本在一秒鐘內暫時變大,然後在下一秒回到較小的尺寸。我怎樣才能防止字體大小跳過我的標籤和倒數計時器?
爲什麼不使用timeIntervalSinceNow? –
良好的通話。更新。 – codeman
你應該看看我的答案,iOS必須更改字體大小的原因是因爲您沒有使用等寬字體:使用相同的字體大小像44:44這樣的文本需要更多的空間來顯示,而不是像文本一樣11:11,iOS調整字體大小以顯示文字。您的主要問題不是自動縮小,而是您的字體不是等寬字體。 –