我有以下代码片段来测试字体功能:
let font = UIFont(name: "MyFont", size: 18)
let features: NSArray = CTFontCopyFeatures(font!)!
print("properties = \(features)")
使用扩展程序使用这些功能:
extension UIFont {
func propotionalNumbers() -> UIFont {
let originalFontDescriptor = self.fontDescriptor
let fontDescriptorFeatureSettings = [
[ UIFontFeatureTypeIdentifierKey: 1,
UIFontFeatureSelectorIdentifierKey: 0],
[ UIFontFeatureTypeIdentifierKey: 16,
UIFontFeatureSelectorIdentifierKey: 4]
]
let fontDescriptorAttributes = [UIFontDescriptorFeatureSettingsAttribute: fontDescriptorFeatureSettings]
let fontDescriptor = originalFontDescriptor.addingAttributes(fontDescriptorAttributes)
let font = UIFont(descriptor: fontDescriptor, size: 0)
return font
}
}
上面的每种字体都很好用,除了下面列出的一种特定字体:
OpenType TrueType
Opentype功能:
然而,这是我在上面的代码片段运行时得到的结果:
properties = (
{
CTFeatureTypeIdentifier = 0;
CTFeatureTypeName = "All Typographic Features";
CTFeatureTypeNameID = 256;
CTFeatureTypeSelectors = (
{
CTFeatureSelectorDefault = 1;
CTFeatureSelectorIdentifier = 0;
CTFeatureSelectorName = "All Type Features";
CTFeatureSelectorNameID = 260;
}
);
},
{
CTFeatureTypeIdentifier = 1;
CTFeatureTypeName = Ligatures;
CTFeatureTypeNameID = 257;
CTFeatureTypeSelectors = (
{
CTFeatureSelectorIdentifier = 4;
CTFeatureSelectorName = "Rare Ligatures";
CTFeatureSelectorNameID = 261;
}
);
},
{
CTFeatureTypeExclusive = 1;
CTFeatureTypeIdentifier = 6;
CTFeatureTypeName = "Number Spacing";
CTFeatureTypeNameID = 258;
CTFeatureTypeSelectors = (
{
CTFeatureSelectorDefault = 1;
CTFeatureSelectorIdentifier = 0;
CTFeatureSelectorName = "Monospaced Numbers";
CTFeatureSelectorNameID = 262;
}
);
},
{
CTFeatureTypeExclusive = 1;
CTFeatureTypeIdentifier = 10;
CTFeatureTypeName = "Vertical Position";
CTFeatureTypeNameID = 259;
CTFeatureTypeSelectors = (
{
CTFeatureSelectorDefault = 1;
CTFeatureSelectorIdentifier = 0;
CTFeatureSelectorName = "Normal Vertical Position";
CTFeatureSelectorNameID = 263;
},
{
CTFeatureSelectorIdentifier = 1;
CTFeatureSelectorName = Superiors;
CTFeatureSelectorNameID = 264;
},
{
CTFeatureSelectorIdentifier = 2;
CTFeatureSelectorName = Inferiors;
CTFeatureSelectorNameID = 265;
}
);
}
)
如您所见,上面列出的大多数功能都缺失了。
如何检查/使用上面列出的Opentype功能?