我正在使用New Font Features at WWDC 2015中的优秀代码:
import UIKit
let pointSize : CGFloat = 60.0
let systemFontDesc = UIFont.systemFont(ofSize: pointSize,
weight: UIFontWeightLight).fontDescriptor
let fractionFontDesc = systemFontDesc.addingAttributes(
[
UIFontDescriptorFeatureSettingsAttribute: [
[
UIFontFeatureTypeIdentifierKey: kFractionsType,
UIFontFeatureSelectorIdentifierKey: kDiagonalFractionsSelector,
],
]
] )
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
label.font = UIFont(descriptor: fractionFontDesc, size:pointSize)
label.text = "12/48"
WWDC视频显示了UIFontDescriptorFeatureSettingsAttribute的其他用途,例如上级(e =mc²)
但解锁其他属性的关键是什么?
UIFontDescriptorFeatureSettingsAttribute的文档页面只是说它是一个包含UIFontFeatureTypeIdentifierKey和UIFontFeatureSelectorIdentifierKey的字典数组。
这两个密钥(type和selector)的文档只是说它们是NSNumber。
对于分数,type为kFractionsType,selector为kDiagonalFractionsSelector
如何找到其他可用功能的密钥?
答案 0 :(得分:1)
Core Text将为您列出字体的隐藏功能:
let desc = UIFontDescriptor(name: "Didot", size: 20) as CTFontDescriptor
let f = CTFontCreateWithFontDescriptor(desc,0,nil)
let arr = CTFontCopyFeatures(f)
print(arr as Any)
对于具有魔术名称的功能,您必须查看 SFNTLayoutTypes.h 标题。