ios - UIFont 等宽数字 + 小型大写字母
问题描述
我正在尝试UIFont
使用以下属性创建一个:
- 大写小写
- 小写小写
- 等宽数字
我正在使用系统字体 ( San Francisco
),它确实支持所有这些功能。
据我所知,唯一的方法是使用多个UIFontDescriptor
.
这是我正在使用的代码:
extension UIFont {
var withSmallCaps: UIFont {
let upperCaseFeature = [
UIFontDescriptor.FeatureKey.featureIdentifier : kUpperCaseType,
UIFontDescriptor.FeatureKey.typeIdentifier : kUpperCaseSmallCapsSelector
]
let lowerCaseFeature = [
UIFontDescriptor.FeatureKey.featureIdentifier : kLowerCaseType,
UIFontDescriptor.FeatureKey.typeIdentifier : kLowerCaseSmallCapsSelector
]
let features = [upperCaseFeature, lowerCaseFeature]
let smallCapsDescriptor = self.fontDescriptor.addingAttributes([UIFontDescriptor.AttributeName.featureSettings : features])
return UIFont(descriptor: smallCapsDescriptor, size: pointSize)
}
var withMonospacedDigits: UIFont {
let monospacedDigitsFeature = [
UIFontDescriptor.FeatureKey.featureIdentifier : kNumberSpacingType,
UIFontDescriptor.FeatureKey.typeIdentifier : kMonospacedNumbersSelector
]
let monospacedDigitsDescriptor = self.fontDescriptor.addingAttributes([UIFontDescriptor.AttributeName.featureSettings : [monospacedDigitsFeature]])
return UIFont(descriptor: monospacedDigitsDescriptor, size: pointSize)
}
}
通过这行代码,我应该能够获得具有前面提到的所有特征的字体:
let font = UIFont.systemFont(ofSize: 16, weight: .regular).withSmallCaps.withMonospacedDigits
// OR
let font = UIFont.monospacedDigitSystemFont(ofSize: 16, weight: .regular).withSmallCaps
但由于某些原因,它不起作用。我无法让字体在使用小写字母的同时具有等宽数字。
我究竟做错了什么?
解决方案
由于@Carpsen90链接的参考文档,我弄清楚了为什么它不起作用。
似乎该Number Spacing
功能是专有的。
如文件所述:
功能分为“独占”和“非独占”。这表明是否可以一次选择给定特征类型内的多个不同选择器。因此,可以同时打开常见和稀有的连字,而不可能同时将给定的分数显示为垂直和对角线。
因此,同时拥有两个等宽数字 + 小型大写字母特征是不可能的。
编辑:
我误读了文件。该功能的选择器是专有的。但不是全部功能。所以应该是可以的。
推荐阅读
- javascript - 如何使用 JS 对 dygraph 中的日期进行降序排序?
- ios - 如何在一个部分中组合相同的日期对象?
- azure - Azure 应用服务未将 Gitlab 容器注册表配置作为私有存储库
- ajax - Magento 2.2 Ajax 在类别页面中第二次无法正常工作
- excel - SQLite3 ODBC 驱动程序 - Access / Excel 逗号而不是点
- android - JobIntentService 不适用于 Oreo 以下的 API (API <= 26)
- java - 侦听器从元素获取文本(如果存在)
- sql - 将部分数据从一个数据库迁移到 SQL Server 中的另一个数据库
- javascript - 如何处理快速路线中的多个参数
- twig - 在 TWIG 中创建包含“raw”功能的过滤器