在 UILabel 中启用小型大写字体
Enabling small caps font in UILabel
我正在尝试在 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"
}
然而,这段代码不起作用,我不明白为什么。我有一些想法:
- 该字体可能不包含小型大写字形。我怀疑这是问题所在,因为我使用的是系统字体。我尝试使用 "Helvetica Neue" 以防万一,但没有成功。
- 使用
UILabel.text
属性 设置文本时不支持小型大写字体。但是,使用 UILabel.attributedText
属性 并不能解决问题。
UILabel
完全不支持小型大写字体。我试了UITextView
,结果还是一样
- 这可能是 iOS 12 的问题,但该问题在 iOS 11.4 上也重现。
还有什么我可能遗漏的吗?
做了几次测试,参考了这个Gist(https://gist.github.com/juliensagot/8fc3e2e6b5ad1e14b3ecb394a417b010),看来你需要设置BOTH大小写特征否则small caps不起作用,至少在模拟器中的 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!"
模拟器截图:
我决定在一个新项目中尝试我的代码,它成功了。然后我从 nib 中删除了 titleLabel
并添加了一个新的,之后我的代码也适用于当前项目。
这似乎是一些奇怪的缓存问题,或者是之前 titleLabel
上的某些设置与小型大写字母字体不兼容。
我正在尝试在 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"
}
然而,这段代码不起作用,我不明白为什么。我有一些想法:
- 该字体可能不包含小型大写字形。我怀疑这是问题所在,因为我使用的是系统字体。我尝试使用 "Helvetica Neue" 以防万一,但没有成功。
- 使用
UILabel.text
属性 设置文本时不支持小型大写字体。但是,使用UILabel.attributedText
属性 并不能解决问题。 UILabel
完全不支持小型大写字体。我试了UITextView
,结果还是一样- 这可能是 iOS 12 的问题,但该问题在 iOS 11.4 上也重现。
还有什么我可能遗漏的吗?
做了几次测试,参考了这个Gist(https://gist.github.com/juliensagot/8fc3e2e6b5ad1e14b3ecb394a417b010),看来你需要设置BOTH大小写特征否则small caps不起作用,至少在模拟器中的 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!"
模拟器截图:
我决定在一个新项目中尝试我的代码,它成功了。然后我从 nib 中删除了 titleLabel
并添加了一个新的,之后我的代码也适用于当前项目。
这似乎是一些奇怪的缓存问题,或者是之前 titleLabel
上的某些设置与小型大写字母字体不兼容。