How to get monospaced numbers in UILabel on iOS 9

前端 未结 8 1195
囚心锁ツ
囚心锁ツ 2020-12-13 06:52

At WWDC 2015, there was a session about the new “San Francisco” system font in iOS 9. It uses proportional number rendering instead of monospaced numbers by default when lin

8条回答
  •  情书的邮戳
    2020-12-13 07:30

    Accepted solution works great, but was crashing with compiler optimization set to Fast(default for Release builds). Rewrote the code like this and now it does not:

    extension UIFont
    {
        var monospacedDigitFont: UIFont
        {
            return UIFont(descriptor: fontDescriptor().fontDescriptorByAddingAttributes([UIFontDescriptorFeatureSettingsAttribute: [[UIFontFeatureTypeIdentifierKey: kNumberSpacingType, UIFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector]]]), size: 0)
        }
    }
    

提交回复
热议问题