SFFontFeatures
为UIFont提供便利扩展,以启用特定于San Francisco字体的功能。
San Francisco字体的许多功能是通过奇异的OpenType设置(例如,交替风格特征)启用的,这些设置在SDK头文件中未命名。此类扩展将该特定抽象化以启用这些功能。
最后,欢迎您向Apple提出建议,在他们的SDK接口中添加更易于启用这些SF字体功能的方式(例如UIKit)。特别是,记录风格选择。
Cocoapods 使用方法
将此行添加到您的 podfile
pod 'SFFontFeatures'
支持 San Francisco 字体功能
有关更多关于 San Francisco 字体的信息,请参阅此处:https://developer.apple.com/fonts/
在代码中使用 SFFontFeatures
Swift
var theFont = UIFont.systemFont(ofSize: 14, weight: UIFontWeightRegular)
theFont = theFont.withTraits(SFFontFeatureTraits.withHighLegibilityEnabled())
或
let traits = SFFontFeatureTraits()
traits.highLegibility = .on
theFont = theFont.withTraits(traits)
Objective-C
UIFont* theFont = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
theFont = [theFont fontWithTraits:[SFFontFeatureTraits traitsWithHighLegibilityEnabled]];
SFFontFeatureTraits* traits;
traits.straightSidedSixAndNine = on;
theFont = [theFont fontWithTraits:traits];
NOTES
NOTE 1: Some of these features are mutually exclusive. e.g. Monospaced Numbers and Proportional numbers.
They are both in the feature list because sometimes you need to turn off default behavior. However,
mixing these two settings On in the same font will probably be undefined. Be careful when using multiple
simultaneous features!
NOTE 2: Some features are enabled by default but require specific character runs for them to be applied.
For example, without turning on the fractional form feature directly, it requires the character run that
is applied this font to be <digits>/<digits>. If the feature doesn't seem to work, check the
character run. Or turn on the feature explicitly.
NOTE 3: Some features are on by default for the SF font. If so, turning on a feature won't have any visible effect.
You can try experimenting turning features on and off to see how they affect the resultant output.
由于上述限制/限制,如果您想在单个字符串中使用多个功能,最好为属性字符串的特定部分启用单个字体功能,而不是在整串字符串中启用多个功能。
功能摘要
以下是支持的列表特性
- 直边六和九
- 开放四
- 垂直居中冒号
- 高可读性
- 一故事a
- 大写字母小写字母
- 小写字母小写字母
- 上下文分数形式
- 等宽/比例数字
- 上标/上标
- 下标/下标