UILabel displaying countdown timer text
我正在使用UILabel显示一个倒数计时器,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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://en.wikipedia.org/wiki/Monospaced_font
您的问题是,使用非固定宽度的字体时,某些数字将大于其他数字。
如果强加了字体,则需要"手动"设置字体大小。
您可能会尝试隐藏UILabel,设置尽可能宽的字符串,例如:
1 | STARTS IN: 44 DAYS 44 HRS 44 MIN ... (assuming 4 is the widest number with your font) |
然后检索使用的字体大小,强制您的应用程序使用。此时无需禁用自动收缩:使用计算出的字体大小,标签应始终适合。
您的字体是所有文字还是仅字词是必需的?您可能还会对数字使用不同的(等距)字体,而其余的则保留其他字体。
还要检查是否已打开或关闭
从理论上讲,iOS仅应将未设置为"固定字体大小"的字体减小为多余的文本。
当宽度不足以显示整个字符串时,iOS会自动缩小。因此,除非修剪它,否则字符串会变小,直到达到最小比例因子/字体大小为止。
因此,这取决于标签的宽度。如果通过自动布局设置其宽度,并且标签无法扩展以适合其内容,则在不够时会缩小字体大小。如果让它扩展以适合内容,标签将始终尝试显示内容而不收缩。