首页 > 解决方案 > Swift 使用小型大写字母

问题描述

如何在 Swift 5 (Storyboard) / Xcode 11 中使用小型大写字母?

这就是我想要完成的事情。

我想出了如何在 SwiftUI 中做到这一点:

Text("Typography")
    .font(Font.system(.body).smallCaps())

但是现在不要在 Storyboard-App 中如何做到这一点。

到目前为止,这是我的代码:

labelBrand.font = UIFont.monospacedSystemFont(ofSize: 14, weight: UIFont.Weight.bold)
labelPorsche.font = UIFont.monospacedSystemFont(ofSize: 18, weight: UIFont.Weight.regular)

谢谢你的帮助。

标签: iosswifttypographyxcode11.4

解决方案


关键是获取您想要的系统字体的字体描述符(我们称之为desc),并将小型大写字母应用为功能:

let d = [
    UIFontDescriptor.FeatureKey.featureIdentifier: kLowerCaseType,
    UIFontDescriptor.FeatureKey.typeIdentifier: kLowerCaseSmallCapsSelector
]
let desc2 = desc.addingAttributes([.featureSettings:[d]])

现在将生成的字体描述符转换desc2回 UIFont。


推荐阅读