我使用这样的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上启用了自动收缩,这样字体就会缩小,在所有设备上看起来都是正确的。我看到的问题是,某些秒和/或分钟会导致字体大小跳来跳去,它将看起来很笨拙,标签文本在一秒内暂时变大,然后在下一秒回到较小的大小。我如何防止字体大小随着我的标签和倒计时计时器而跳动?
发布于 2015-06-29 22:13:52
你应该使用固定宽度的“等宽”字体,如Consolas,Courier,DejaVu Sans Mono,Letter哥特式,解放Mono,Mongo...
https://en.wikipedia.org/wiki/Monospaced_font
你的问题是,使用非固定宽度字体时,一些数字会比其他数字大。
如果你的字体是强制的,你需要“手动”强制你的字体大小。你可以试着隐藏你的UILabel,设置最宽的字符串,例如:
STARTS IN: 44 DAYS 44 HRS 44 MIN ... (assuming 4 is the widest number with your font)然后检索使用的字体大小,强制您的应用程序使用。此时禁用自动收缩是不必要的:使用计算的字体大小,标签应该始终适合。
您的字体对所有文本都是强制的,还是只对单词强制?您也可以对数字使用不同的(等宽)字体,并将您的字体保留为其他字体。
发布于 2015-06-29 22:12:33
当宽度不足以显示整个字符串时,iOS会自动收缩。因此,字符串get将变小,直到达到最小比例因子/字体大小,而不是对其进行修剪。
因此,这取决于标签的宽度。如果你通过自动布局来设置它的宽度,并且标签不能扩展到适合它的内容,当这还不够时,它会缩小字体大小。如果您让它扩展以适合内容,标签将始终尝试显示内容而不缩小。
发布于 2015-06-29 22:26:16
还要检查您是否打开或关闭了AutoShrink。

理论上,如果没有设置为“固定字体大小”,iOS应该只减小字体大小以挤入额外的文本。
https://stackoverflow.com/questions/31117818
复制相似问题