【问题标题】:iOS Monospaced Custom FontiOS 等宽自定义字体
【发布时间】:2016-04-06 21:36:49
【问题描述】:

我的 Xcode 7、iOS 9 目标项目中包含自定义字体。我想让字体等宽。我试过了,但没有用:

let originalFont = UIFont(name: "My Custom Font", size: 18)
let originalFontDescriptor = originalFont!.fontDescriptor()

let fontDescriptorFeatureSettings = [
    [
        UIFontFeatureTypeIdentifierKey: kNumberSpacingType,
        UIFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector
    ]
]

let fontDescriptorAttributes = [UIFontDescriptorFeatureSettingsAttribute: fontDescriptorFeatureSettings]
let fontDescriptor = originalFontDescriptor.fontDescriptorByAddingAttributes(fontDescriptorAttributes)
let font = UIFont(descriptor: fontDescriptor, size: 0)

topLabel.font = font

无论有没有上述代码,标签都会以适当的自定义字体显示。只是上面的代码没有做任何事情。

【问题讨论】:

  • 代码看起来可以工作。会不会是字体设置的位置不对?你在哪里做这个?
  • 这就是我要去的地方。如何获取字体设置的位置?
  • 我不认为非等宽字体可以简单地用作等宽字体。字体要么是等宽的,要么不是。

标签: ios fonts core-text monospace


【解决方案1】:

我的以下答案只是将现有字体的数字(不是整个字体)设为等宽(如果字体支持的话)

至少我在找到此线程时正在寻找使数字等宽的方法。所以我希望它会有所帮助,尽管它回答了另一个问题。

这很好用,在 Swift 5 和 iOS14+13 上测试过:

(只要“您的字体支持等宽数字功能”。)

extension UIFont {

    var monospacedDigitFont: UIFont {
        let oldFontDescriptor = fontDescriptor
        let newFontDescriptor = oldFontDescriptor.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
    }
}

然后你可以像这样在任何标签上使用它:

/// Label with monospacing activated
myLabel.font = myLabel.font.monospacedDigitFontDescriptor

/// Label with monospacing not activated (default is proportional spacing)
myLabel.font = myLabel.font

(来源:https://blog.usejournal.com/proportional-vs-monospaced-numbers-when-to-use-which-one-in-order-to-avoid-wiggling-labels-e31b1c83e4d0)

【讨论】:

    【解决方案2】:

    您使用的代码没有使字体等宽。

    它正在调整字体以在等宽模式下呈现数字。所以所有具有这种字体的数字都将具有相同的宽度。

    下面是一个有 4 个标签的例子,1 使用自定义字体 Docis Light,2 使用等宽数字的 Docis Light,3 是相同大小的系统字体,4 是使用等宽数字的系统字体:

    如您所见,这种自定义字体已经支持开箱即用的等宽数字功能,无需调整。

    如果您需要使用等宽字体(不仅仅是数字),您必须使用自定义等宽字体(设计为等宽字体),或者您可以使用内置的 iOS 等宽字体,例如 Courier 或 Menlo(查看所有可用的 iOS 字体http://iosfonts.com/)

    这是它们在相同场景下的样子:

    无论是否调整,它们已经是等宽的,并且数字也是等宽的。

    我在这里回答了类似的问题,可能我应该只链接answer 而不是图片,但它更直观。

    【讨论】:

      【解决方案3】:

      不要忘记导入 header 文件。希望它会奏效。此解决方案在 Objective-C

      中
      #import <CoreTextArcView.h>
      
      UIFont *const existingFont = [UIFont preferredFontForTextStyle:   UIFontTextStyleBody];
      UIFontDescriptor *const existingDescriptor = [existingFont fontDescriptor];
      
      NSDictionary *const fontAttributes = @{
      
      UIFontFeatureTypeIdentifierKey 
      
      UIFontDescriptorFeatureSettingsAttribute: @[
       @{
         UIFontFeatureTypeIdentifierKey: @(kNumberSpacingType),
         UIFontFeatureSelectorIdentifierKey: @(kMonospacedNumbersSelector)
        }]
      };
      
      UIFontDescriptor *const monospacedDescriptor = [existingDescriptor  fontDescriptorByAddingAttributes: fontAttributes];
      UIFont *const proportionalFont = [UIFont fontWithDescriptor: monospacedDescriptor size: [existingFont pointSize]];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-06
        • 1970-01-01
        • 2012-04-15
        • 1970-01-01
        • 1970-01-01
        • 2013-01-09
        • 2020-12-17
        相关资源
        最近更新 更多