如何在iOS 9上的UILabel中获取等宽数字

时间:2015-06-15 20:46:38

标签: ios uilabel uifont monospace ios9

iOS 9中的

At WWDC 2015, there was a session about the new “San Francisco” system font默认情况下,当与iOS 9 SDK链接时,它使用比例数字渲染而不是等宽数字。 NSFont上有一个方便的初始化程序,名为NSFont.monospacedDigitsSystemFontOfSize(mySize weight:),可用于显式启用等宽数字显示。

但是我无法在UIKit上找到与UIFont相当的内容。

8 个答案:

答案 0 :(得分:48)

现在,从iOS 9开始,UIFont可以使用此功能:

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

例如:

[UIFont monospacedDigitSystemFontOfSize:42.0 weight:UIFontWeightMedium];

或在Swift中:

UIFont.monospacedDigitSystemFont(ofSize: 42.0, weight: UIFontWeightMedium)

答案 1 :(得分:40)

Handy 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上的最新版本。

答案 2 :(得分:5)

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

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

答案 3 :(得分:5)

Swift 4中有一些重命名,所以属性现在看起来像这样:

    let fontDescriptorAttributes = [
        UIFontDescriptor.AttributeName.featureSettings: [
            [
                UIFontDescriptor.FeatureKey.featureIdentifier: kNumberSpacingType,
                UIFontDescriptor.FeatureKey.typeIdentifier: kMonospacedNumbersSelector
            ]
        ]
    ]

答案 4 :(得分:2)

注意:当前接受的答案中的方法已经在Xcode 7.3(Swift 2.2)中开始崩溃,仅在Release版本中。消除中间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)
    }
}

答案 5 :(得分:1)

accepted answer之后使用动态类型的 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)

答案 6 :(得分: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
    }
}

答案 7 :(得分:-3)

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