How to get monospaced numbers in UILabel on iOS 9

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

    This is now available in UIFont since iOS 9:

    + (UIFont *)monospacedDigitSystemFontOfSize:(CGFloat)fontSize weight:(CGFloat)weight NS_AVAILABLE_IOS(9_0);
    

    eg:

    [UIFont monospacedDigitSystemFontOfSize:42.0 weight:UIFontWeightMedium];
    

    or in Swift:

    UIFont.monospacedDigitSystemFont(ofSize: 42.0, weight: UIFontWeightMedium)
    
    0 讨论(0)
  • 2020-12-13 07:39

    A bit improved version of the @Rudolf Adamkovic code which checks iOS version:

    var monospacedDigitFont: UIFont {
    
        if #available(iOS 9, *) {
            let oldFontDescriptor = fontDescriptor()
            let newFontDescriptor = oldFontDescriptor.monospacedDigitFontDescriptor
    
            return UIFont(descriptor: newFontDescriptor, size: 0)
        } else {
           return self
        }
    }
    
    0 讨论(0)
提交回复
热议问题