How to get monospaced numbers in UILabel on iOS 9

前端 未结 8 1196
囚心锁ツ
囚心锁ツ 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:15

    Example usage for Swift 5.2 following the accepted answer using dynamic type.

    label.font = .init(descriptor: UIFont.preferredFont(forTextStyle: .body)
                     .fontDescriptor.addingAttributes([
                     .featureSettings: [[
                         UIFontDescriptor.FeatureKey.featureIdentifier: kNumberSpacingType,
                                                    .typeIdentifier: kMonospacedNumbersSelector]]]),
                                                    size: 0)
    

    Worth mentioning for macOS (AppKit) it is slightly different:

    NSFont(descriptor: NSFont.systemFont(ofSize: 20).fontDescriptor
           .addingAttributes([.featureSettings: [[NSFontDescriptor.FeatureKey
           .selectorIdentifier: kMonospacedNumbersSelector,
           .typeIdentifier: kNumberSpacingType]]]), size: 0)
    

提交回复
热议问题