如何在iOS故事板中使用自定义字体来表示动态类型辅助功能大小

tim*_*ith 11 ios uistoryboard xcode-storyboard swift

如何使用动态类型文本样式 "Title 1"并将字体面设置为故事板中UILabel的内置字体Chalkboard SE?

我需要尊重iOS中的动态类型大小(Apple自iOS 7以来一直鼓励这样做吗?)我还需要使用内置字体Chalkboard SE,默认情况下它不用于"文本样式"字体.我目前正在使用图像中显示的自定义字体,但需要字体根据用户的动态类型/辅助功能大小首选项更改大小,就像所有文本样式字体一样.最佳文本样式选项是标题1,但字体/字体是不可接受的.

Xcode中的字体菜单. 自定义选中并突出显示标题1

小智 19

虽然您无法通过Storyboard指定自定义字体和首选文本样式,但以编程方式为自定义字体指定动态类型大小并不困难:

迅速:

let pointSize  = UIFontDescriptor.preferredFontDescriptorWithTextStyle(UIFont??TextStyleTitle1).poi??ntSize
let customFont = UIFont(name: "Chalkboard SE", size: pointSize)
Run Code Online (Sandbox Code Playgroud)

收到a时UIContentSizeCategoryDidChangeNotification,请使用相同的代码更新标签的字体.

对象C:

 CGFloat pointSize = [[UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleHeadline] pointSize];
 [titleLabel setFont:[UIFont fontWithName:@"Marker Felt" size:pointSize]];
Run Code Online (Sandbox Code Playgroud)

  • `let pointSize = UIFontDescriptor.preferredFontDescriptorWithTextStyle(UIFontTextStyleTitle1).pointSize`应该是首选,因为初始化`UIFont`更贵. (4认同)