mok*_*gio 4 uifont ios uifontdescriptor
UIFont
提供了+preferredFontForTextStyle:
获得字体实例与适当大小的方法,根据用户选择的内容大小类别和给定UIFontTextStyle
。
我想做的是为给定的文本样式和内容大小获取字体。类似的东西+fontForContentSizeCategory:andTextStyle:
。
不幸的是,我在UIFont
或的标题中找不到任何类似的内容UIFontDescriptor
。
关于如何实现这一目标的任何想法?
谢谢
不幸的是,+[UIFont preferredFontForTextStyle:]
并+[UIFontDescriptor preferredFontDescriptorWithTextStyle:]
依靠-[UIApplication preferredContentSizeCategory]
内部。他们使用私有函数_CTFontDescriptorCreateWithTextStyle
来检索具有特定文本样式和大小类别的 CoreText 字体描述符,并且最终基于类别 ? 来自CoreTextConfig.plist
存储在某处的配置文件的大小映射,但我假设您不想使用私有 API。
虽然犹豫地暗示有可能动态 swizzle-[UIApplication preferredContentSizeCategory]
以欺骗+[UIFontDescriptor preferredFontDescriptorWithTextStyle:]
返回您想要的大小类的字体描述符,但我不能推荐任何特定的方法。您可以像这样检索字体描述符:
let descriptor = UIFontDescriptor(fontAttributes: [ UIFontDescriptorTextStyleAttribute : UIFontTextStyleBody ])
Run Code Online (Sandbox Code Playgroud)
但它不会包含 size 属性,因此您将不得不尝试提出一个类别?大小映射自己。
从 iOS 10.0 开始,这可以使用UITraitCollection
:
let traitCollection = UITraitCollection(preferredContentSizeCategory: contentSizeCategory)
let font = UIFont.preferredFont(forTextStyle: textStyle, compatibleWith: traitCollection)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1068 次 |
最近记录: |