UILabel显示倒数计时器文本

时间:2015-06-29 14:06:33

标签: ios swift

我正在使用像这样的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上启用了自动收缩,因此字体将缩小并在所有设备上都能正常显示。我看到的问题是,某些秒和/或分钟会导致字体大小跳跃,标签文本在一秒钟内暂时变大,然后在下一秒返回到较小的尺寸时看起来很尴尬。如何防止字体大小随我的标签跳转并倒计时?

3 个答案:

答案 0 :(得分:1)

你应该使用" monospaced"固定宽度的字体,如Consolas,Courier,DejaVu Sans Mono,Letter Gothic,Liberation Mono,Monaco ...

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)

然后检索使用的字体大小,强制您的应用程序使用。此时禁用自动收缩是不必要的:使用计算的字体大小,标签应始终适合。

您的字体对所有文字是强制性的还是只对单词有效?您也可以使用不同的(等宽字体)字体表示数字,并保留其余部分。

答案 1 :(得分:0)

当宽度不足以显示整个字符串时,iOS自动收缩。因此,不是修剪它,字符串变小,直到最小比例因子/字体大小。

所以这取决于标签的宽度。如果您通过自动布局设置宽度,并且标签无法展开以适应其内容,那么当标签不够时,它会缩小字体大小。如果您扩展以适应内容,标签将始终尝试显示内容而不缩小。

答案 2 :(得分:0)

还要检查您是否已开启或关闭if($contact->CustomFields->c->ask_again->LookupName){ $Contactname=$contact->CustomFields->c->ask_again->LookupName; }

AutoShrink

理论上,如果此设置为“固定字体大小”,iOS应该只减小字体大小以挤入额外的文本。