[英]UILabel displaying countdown timer text
我正在使用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上啟用了自動收縮功能,以便字體將縮小並在所有設備上正確顯示。 我看到的問題是,某些秒和/或分鍾會導致字體大小跳動,並且看起來有點尷尬,因為標簽文本在一秒鍾內暫時變大,然后在下一秒鍾又變小。 如何防止字體大小隨標簽跳動並倒計時?
您應該使用固定寬度的“等寬”字體,例如Consolas,Courier,DejaVu Sans Mono,Letter Gothic,Liberation Mono,Monaco ...
https://zh.wikipedia.org/wiki/Monospaced_font
您的問題是,使用非固定寬度的字體時,某些數字會大於其他數字。
如果強加了字體,則需要“手動”設置字體大小。 您可能會嘗試隱藏UILabel,設置盡可能寬的字符串,例如:
STARTS IN: 44 DAYS 44 HRS 44 MIN ... (assuming 4 is the widest number with your font)
然后檢索使用的字體大小,強制您的應用程序使用。 此時無需禁用自動收縮功能:使用計算出的字體大小,標簽應始終適合。
您的字體是所有文本還是僅單詞必須的字體? 您可能還會對數字使用其他(等距)字體,而其余的則保留。
當寬度不足以顯示整個字符串時,iOS自動縮小。 因此,直到最小比例因子/字體大小為止,字符串都會變小,而不是進行修剪。
因此,這取決於標簽的寬度。 如果通過自動布局設置它的寬度,並且標簽無法擴展以適合其內容,則在不夠時會縮小字體大小。 如果讓它擴展以適合內容,標簽將始終嘗試顯示內容而不收縮。
還要檢查是否已打開或關閉“自動AutoShrink
。
從理論上講,iOS如果未將其設置為“固定字體大小”,則應僅減小字體大小以擠入多余的文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.