当数字更改时,iOS防止计时器UILabel``晃动'' [英] iOS Prevent timer UILabel 'shaking' when numbers change
问题描述
我有一个UILabel
,它以MM:ss:SS
格式(分钟,秒,厘秒)显示计时器的输出,但是随着厘秒宽度的变化,它从左向右摇动-"11"比"33"窄.
I have a UILabel
which shows the output of a timer in the format MM:ss:SS
(minutes, seconds, centiseconds), however it "shakes" from left to right as the width of the centiseconds changes - "11" is narrower than "33" for example.
有什么办法可以减轻这种情况?我尝试将它居中,给它固定的宽度,但是它们似乎并没有帮助.
Is there any way I can mitigate this? I've tried centring it, giving it a fixed width but they haven't seemed to help.
推荐答案
自iOS 9.0起,系统字体使用比例数字.如果需要等宽数字,可以使用+[UIFont monospacedDigitSystemFontOfSize:weight:]
获得一种变体字体.这仅适用于系统字体.
Since iOS 9.0, the system font uses proportional digits. If you want monospaced digits, there's a variant font which you can obtain using +[UIFont monospacedDigitSystemFontOfSize:weight:]
. This only works for the system font.
如果要使用另一种字体,则尝试要求等宽的变体,但可能没有.给定一个UIFont
,您可以请求它的fontDescriptor
,然后要求使用-[UIFontDescriptor fontDescriptorWithSymbolicTraits:]
和UIFontDescriptorTraitMonoSpace
等宽(而不是数字)的类似字体描述符.然后,您可以通过将新的字体描述符传递到+[UIFont fontWithDescriptor:size:]
来创建新的字体.
If you want to work with another font, you try to ask for a monospaced variant, but there may not be one. Given a UIFont
, you can request its fontDescriptor
, then ask that for a similar font descriptor that's monospaced (not just for digits) using -[UIFontDescriptor fontDescriptorWithSymbolicTraits:]
and UIFontDescriptorTraitMonoSpace
. You can then create a new font by passing the new font descriptor to +[UIFont fontWithDescriptor:size:]
.
但是,我怀疑是否有Impact的等宽变体.这不适合您的目的.
However, I doubt there's a monospace variant of Impact. It's not suitable for your purpose.
这篇关于当数字更改时,iOS防止计时器UILabel``晃动''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!