首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在iOS 13上创建小Caps选择器

无法在iOS 13上创建小Caps选择器
EN

Stack Overflow用户
提问于 2019-09-30 11:45:29
回答 3查看 651关注 0票数 2

下面的代码使我能够在iOS 12上创建大写文本,但是在iOS 13上,它停止工作了。

iOS 12日志

代码语言:javascript
复制
.SFUIDisplay-Semibold
.SFUIDisplay-Semibold

iOS 13测井

代码语言:javascript
复制
.SFUI-Semibold
TimesNewRomanPSMT

看起来SFUI字体至少在iOS 13中已经更改了名称,但是它是否也放弃了对小盘的支持?

代码语言:javascript
复制
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)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-09-30 14:31:22

你的代码总是错的。您首先创建一个字体,然后通过传递字体的名称从它派生一个字体描述符

代码语言:javascript
复制
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中工作,现在起作用了:

代码语言:javascript
复制
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)
票数 3
EN

Stack Overflow用户

发布于 2019-09-30 12:39:32

像这样的系统字体现在可以使用“点”/ .符号(即使像font.fontName示例一样重新创建它们)来进行更长的引用,如下所示:

https://developer.apple.com/videos/play/wwdc2019/227/

从iOS 13开始,您可以使用新的枚举UIFontDescriptor.SystemDesign,使用regularroundedserifmonospaced

关于如何在Swift中使用描述符创建字体的示例(请参阅我如何使用design参数):

代码语言:javascript
复制
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)
    }
}
票数 2
EN

Stack Overflow用户

发布于 2019-09-30 13:44:59

从@Ash Camerons的答案中汲取灵感,下面是更新的代码,它允许您应用小盘

注意,这只在iOS 13+中有效,但是您可以为iOS 13和更高版本添加.withDesign(systemDesign)行。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58167269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档