在 UILabel 中启用小型大写字母字体

And*_*nko 1 typography uilabel core-text ios

我正在尝试在UILabel. 这个问题之前已经被问过很多次了,答案很简单:

override func awakeFromNib() {
  super.awakeFromNib()

  let fontSize: CGFloat = 24
  let descriptor = UIFont
    .systemFont(ofSize: 24)
    .fontDescriptor
    .addingAttributes([
      UIFontDescriptor.AttributeName.featureSettings: [
        UIFontDescriptor.FeatureKey.featureIdentifier: kLowerCaseType,
        UIFontDescriptor.AttributeName.featureSettings: kLowerCaseSmallCapsSelector
      ]
    ])

  titleLabel.font = UIFont(descriptor: descriptor, size: fontSize)
  titleLabel.text = "Welcome"
}
Run Code Online (Sandbox Code Playgroud)

然而,这段代码不起作用,我不明白为什么。我有几个想法:

  1. 该字体可能不包含小型大写字母字形。我怀疑这是问题,因为我使用的是系统字体。我尝试使用“Helvetica Neue”以防万一,但没有成功。
  2. 使用该属性设置文本时不支持小型大写字体UILabel.text。然而,使用该UILabel.attributedText属性并不能解决问题。
  3. UILabel根本不支持小型大写字体。我尝试过UITextView,但结果是一样的。
  4. 这可能是 iOS 12 的问题,但该问题在 iOS 11.4 上也会重现。

还有什么我可能想念的吗?

Jon*_*onJ 5

做了一些测试,并参考这个要点(https://gist.github.com/juliensagot/8fc3e2e6b5ad1e14b3ecb394a417b010),似乎你需要设置大写和小写功能,否则小型大写字母至少不起作用在模拟器中的 iOS 12 下。使用几种不同的字体进行了测试。

let fontSize: CGFloat = 24.0
let fontDescriptor = UIFont.systemFont(ofSize: fontSize, weight: .medium).fontDescriptor

let upperCaseFeature = [
    UIFontDescriptor.FeatureKey.featureIdentifier : kUpperCaseType,
    UIFontDescriptor.FeatureKey.typeIdentifier : kUpperCaseType
 ]

let lowerCaseFeature = [
    UIFontDescriptor.FeatureKey.featureIdentifier : kLowerCaseType,
    UIFontDescriptor.FeatureKey.typeIdentifier : kLowerCaseSmallCapsSelector
]

let features = [upperCaseFeature, lowerCaseFeature]
let additions = fontDescriptor.addingAttributes([.featureSettings: features])

label.font = UIFont(descriptor: additions, size: fontSize)
label.text = "Hello There!"
Run Code Online (Sandbox Code Playgroud)

模拟器截图:

模拟器的屏幕截图