36

在 WWDC 2015 上,有一个关于 iOS 9 中新的“旧金山”系统字体的会议。它在与 iOS 9 SDK 链接时默认使用比例数字呈现而不是等宽数字。NSFont 上有一个方便的初始化程序NSFont.monospacedDigitsSystemFontOfSize(mySize weight:),可用于显式启用等宽数字显示。

但是我UIKitUIFont.

4

8 回答 8

56

UIFont从 iOS 9 开始就可以使用了:

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

例如:

[UIFont monospacedDigitSystemFontOfSize:42.0 weight:UIFontWeightMedium];

或在斯威夫特:

UIFont.monospacedDigitSystemFont(ofSize: 42.0, weight: UIFontWeightMedium)
于 2015-08-29T11:42:55.690 回答
53

方便的UIFont扩展:

extension UIFont {
    var monospacedDigitFont: UIFont {
        let newFontDescriptor = fontDescriptor.monospacedDigitFontDescriptor
        return UIFont(descriptor: newFontDescriptor, size: 0)
    }
}

private extension UIFontDescriptor {
    var monospacedDigitFontDescriptor: UIFontDescriptor {
        let fontDescriptorFeatureSettings = [[UIFontDescriptor.FeatureKey.featureIdentifier: kNumberSpacingType,
                                              UIFontDescriptor.FeatureKey.typeIdentifier: kMonospacedNumbersSelector]]
        let fontDescriptorAttributes = [UIFontDescriptor.AttributeName.featureSettings: fontDescriptorFeatureSettings]
        let fontDescriptor = self.addingAttributes(fontDescriptorAttributes)
        return fontDescriptor
    }
}

使用@IBOutlet属性:

@IBOutlet private var timeLabel: UILabel? {
    didSet {
        timeLabel.font = timeLabel.font.monospacedDigitFont
    }
}

GitHub 上的最新版本。

于 2015-06-22T13:45:21.170 回答
5

接受的解决方案效果很好,但是在编译器优化设置为快速(发布版本的默认设置)时崩溃。像这样重写代码,现在它没有:

extension UIFont
{
    var monospacedDigitFont: UIFont
    {
        return UIFont(descriptor: fontDescriptor().fontDescriptorByAddingAttributes([UIFontDescriptorFeatureSettingsAttribute: [[UIFontFeatureTypeIdentifierKey: kNumberSpacingType, UIFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector]]]), size: 0)
    }
}
于 2016-07-25T13:40:51.930 回答
5

在 Swift 4 中进行了相当多的重命名,所以属性现在看起来像这样:

    let fontDescriptorAttributes = [
        UIFontDescriptor.AttributeName.featureSettings: [
            [
                UIFontDescriptor.FeatureKey.featureIdentifier: kNumberSpacingType,
                UIFontDescriptor.FeatureKey.typeIdentifier: kMonospacedNumbersSelector
            ]
        ]
    ]
于 2018-05-28T08:59:30.107 回答
3

注意:当前接受的答案中的方法在 Xcode 7.3 (Swift 2.2) 中对我来说已经开始崩溃,仅在发布版本中。消除中间monospacedDigitFontDescriptor扩展变量解决了这个问题。

extension UIFont {
    var monospacedDigitFont: UIFont {
        let fontDescriptorFeatureSettings = [[UIFontFeatureTypeIdentifierKey: kNumberSpacingType, UIFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector]]
        let fontDescriptorAttributes = [UIFontDescriptorFeatureSettingsAttribute: fontDescriptorFeatureSettings]
        let oldFontDescriptor = fontDescriptor()
        let newFontDescriptor = oldFontDescriptor.fontDescriptorByAddingAttributes(fontDescriptorAttributes)

        return UIFont(descriptor: newFontDescriptor, size: 0)
    }
}
于 2016-03-29T02:33:15.920 回答
1

接受的答案之后使用动态类型的Swift 5.2的示例用法。

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

值得一提的是 macOS (AppKit) 略有不同:

NSFont(descriptor: NSFont.systemFont(ofSize: 20).fontDescriptor
       .addingAttributes([.featureSettings: [[NSFontDescriptor.FeatureKey
       .selectorIdentifier: kMonospacedNumbersSelector,
       .typeIdentifier: kNumberSpacingType]]]), size: 0)
于 2020-05-26T12:10:26.697 回答
0

检查 iOS 版本的 @Rudolf Adamkovic 代码的一些改进版本:

var monospacedDigitFont: UIFont {

    if #available(iOS 9, *) {
        let oldFontDescriptor = fontDescriptor()
        let newFontDescriptor = oldFontDescriptor.monospacedDigitFontDescriptor

        return UIFont(descriptor: newFontDescriptor, size: 0)
    } else {
       return self
    }
}
于 2015-10-02T15:40:36.043 回答
-3

或者,只需使用 Helvetica。它仍然具有等宽数字,并且可以追溯至旧的 iOS 版本。

于 2016-04-14T17:01:09.630 回答