1

我的 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

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

4

3 回答 3

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 )

于 2020-10-01T13:28:00.693 回答
0

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

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

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

ios中的等宽数字字体调整

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

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

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

courier 和 menlo 等宽 iOS 字体

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

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

于 2016-10-14T05:04:47.833 回答
0

不要忘记导入文件。希望它会奏效。此解决方案在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]];
于 2018-06-11T07:33:45.343 回答