下面的代码使我能够在iOS 12上创建小写字母文本。但是,在iOS 13上,该文本已停止工作。
iOS 12日志
.SFUIDisplay-Semibold
.SFUIDisplay-Semibold
iOS 13日志
.SFUI-Semibold
TimesNewRomanPSMT
似乎SFUI字体至少在iOS 13中已更改名称,但它是否也放弃了对小写字母的支持?
let font = UIFont.systemFont(ofSize: 20, weight: .semibold)
print(font.fontName)
let settings = [[UIFontDescriptor.FeatureKey.featureIdentifier: kLowerCaseType,
UIFontDescriptor.FeatureKey.typeIdentifier: kLowerCaseSmallCapsSelector]]
let attributes: [UIFontDescriptor.AttributeName: AnyObject] = [UIFontDescriptor.AttributeName.featureSettings: settings as AnyObject,
UIFontDescriptor.AttributeName.name: font.fontName as AnyObject]
let smallCapsFont = UIFont(descriptor: UIFontDescriptor(fontAttributes: attributes), size: 20)
print(smallCapsFont.fontName)
现在这样的系统字体现在可以用'。'/ .
表示法引用,甚至可以像font.fontName
那样重新创建:)
https://developer.apple.com/videos/play/wwdc2019/227/
[从iOS 13开始,您可以使用UIFontDescriptor.SystemDesign
,regular
,rounded
或serif
使用新的枚举monospaced
。
[如何在Swift中使用描述符创建字体的示例(请参阅我如何使用design
参数):
extension UIFont {
convenience init?(
style: UIFont.TextStyle,
weight: UIFont.Weight = .regular,
design: UIFontDescriptor.SystemDesign = .default) {
guard let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style)
.addingAttributes([UIFontDescriptor.AttributeName.traits: [UIFontDescriptor.TraitKey.weight: weight]])
.withDesign(design) else {
return nil
}
self.init(descriptor: descriptor, size: 0)
}
}
从@Ash Camerons的答案中汲取灵感,这是更新的代码,可让您使用小写字母