在WWDC 2015上,有一个关于iOS 9中新的"旧金山"系统字体的会议。默认情况下,当与 iOS 9 SDK 链接时,它使用比例数字呈现而不是等宽数字。NSFont 上有一个名为 NSFont.monospacedDigitsSystemFontOfSize(mySize weight:)
的方便的初始值设定项,可用于显式启用等宽数字显示。
但是,我在UIFont
上找不到UIKit
等效物。
自 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)
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 上的最新版本。
接受的解决方案效果很好,但是编译器优化设置为快速(发布版本的默认值)时崩溃了。像这样重写了代码,现在它没有:
extension UIFont
{
var monospacedDigitFont: UIFont
{
return UIFont(descriptor: fontDescriptor().fontDescriptorByAddingAttributes([UIFontDescriptorFeatureSettingsAttribute: [[UIFontFeatureTypeIdentifierKey: kNumberSpacingType, UIFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector]]]), size: 0)
}
}
在 Swift 4 中已经进行了相当多的重命名,因此属性现在如下所示:
let fontDescriptorAttributes = [
UIFontDescriptor.AttributeName.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kNumberSpacingType,
UIFontDescriptor.FeatureKey.typeIdentifier: kMonospacedNumbersSelector
]
]
]
注意:当前接受的答案中的方法在 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)
}
}
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)
检查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
}
}
,只使用Helvetica。它仍然具有等宽数字,并且可以追溯到较旧的iOS版本。