下面的代码使我能够在iOS 12上创建大写文本,但是在iOS 13上,它停止工作了。
iOS 12日志
.SFUIDisplay-Semibold
.SFUIDisplay-SemiboldiOS 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)发布于 2019-09-30 14:31:22
你的代码总是错的。您首先创建一个字体,然后通过传递字体的名称从它派生一个字体描述符
let font = UIFont.systemFont(ofSize: 20, weight: .semibold)
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 // ** NO!
]
let smallCapsFont = UIFont(descriptor: UIFontDescriptor(fontAttributes: attributes), size: 20)那从来都不是正确的。您应该直接从字体本身派生字体描述符。这就是您的代码应该始终看起来的样子。它在iOS 12中工作,现在起作用了:
let font = UIFont.systemFont(ofSize: 20, weight: .semibold)
let settings = [
[UIFontDescriptor.FeatureKey.featureIdentifier: kLowerCaseType,
UIFontDescriptor.FeatureKey.typeIdentifier: kLowerCaseSmallCapsSelector]
]
let desc = font.fontDescriptor.addingAttributes([.featureSettings:settings])
let smallCapsFont = UIFont(descriptor: desc, size: 0)发布于 2019-09-30 12:39:32
像这样的系统字体现在可以使用“点”/ .符号(即使像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)
}
}发布于 2019-09-30 13:44:59
从@Ash Camerons的答案中汲取灵感,下面是更新的代码,它允许您应用小盘
注意,这只在iOS 13+中有效,但是您可以为iOS 13和更高版本添加.withDesign(systemDesign)行。
let pointSize: CGFloat = 20
let weight: UIFont.Weight = .semibold
let systemDesign: UIFontDescriptor.SystemDesign = .default
let settings = [[UIFontDescriptor.FeatureKey.featureIdentifier: kLowerCaseType,
UIFontDescriptor.FeatureKey.typeIdentifier: kLowerCaseSmallCapsSelector]]
let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .callout)
.addingAttributes([UIFontDescriptor.AttributeName.traits: [UIFontDescriptor.TraitKey.weight: weight]])
.addingAttributes([UIFontDescriptor.AttributeName.featureSettings: settings])
.addingAttributes([UIFontDescriptor.AttributeName.size: pointSize])
.withDesign(systemDesign)
let font = UIFont(descriptor: descriptor!, size: pointSize)https://stackoverflow.com/questions/58167269
复制相似问题