UIFont
提供了+preferredFontForTextStyle:
根据用户选择的内容大小类别和给定的UIFontTextStyle
.
我想做的是为给定的文本样式和内容大小获取字体。类似的东西+fontForContentSizeCategory:andTextStyle:
。
UIFont
不幸的是,我在or的标题中找不到类似的东西UIFontDescriptor
。
关于如何实现这一目标的任何想法?
谢谢
UIFont
提供了+preferredFontForTextStyle:
根据用户选择的内容大小类别和给定的UIFontTextStyle
.
我想做的是为给定的文本样式和内容大小获取字体。类似的东西+fontForContentSizeCategory:andTextStyle:
。
UIFont
不幸的是,我在or的标题中找不到类似的东西UIFontDescriptor
。
关于如何实现这一目标的任何想法?
谢谢
从 iOS 10.0 开始,这可以使用UITraitCollection
:
let traitCollection = UITraitCollection(preferredContentSizeCategory: contentSizeCategory)
let font = UIFont.preferredFont(forTextStyle: textStyle, compatibleWith: traitCollection)
不幸的是,两者都+[UIFont preferredFontForTextStyle:]
依赖+[UIFontDescriptor preferredFontDescriptorWithTextStyle:]
于-[UIApplication preferredContentSizeCategory]
内部。他们使用私有函数_CTFontDescriptorCreateWithTextStyle
来检索具有特定文本样式和大小类别的 CoreText 字体描述符,并且最终基于CoreTextConfig.plist
存储在某处的配置文件中的类别 → 大小映射,但我假设您不想使用私有 API。
虽然犹豫暗示有可能动态-[UIApplication preferredContentSizeCategory]
调整以欺骗+[UIFontDescriptor preferredFontDescriptorWithTextStyle:]
返回您想要的大小类的字体描述符,但我不能推荐任何具体的方法。您可以像这样检索字体描述符:
let descriptor = UIFontDescriptor(fontAttributes: [ UIFontDescriptorTextStyleAttribute : UIFontTextStyleBody ])
但它不包含大小属性,因此您将不得不自己尝试提出类别→大小映射。