How to measure dynamic type accessibility dimensions using a special font in iOS storyboard

How can I use the style of the dynamic text type "Title 1" and set the font to face the embedded Chalkboard SE font for UILabel in the storyboard?

I need to estimate the size of the dynamic type in iOS (Apple encouraged this with iOS 7?) I also need to use the built-in Chalkboard SE font, which is not used by default for "text style" fonts. Currently I use a special font, as shown in the image , but changing the font size requires a font size according to the user type Dynamic Type / Accessibility Sizes, just like all Text Styles fonts. The best option for text styles is Title 1, but the font / font is not acceptable.

The font menu in Xcode. Custom checkbox and header selection 1> </a> </p></div></body> </html>

+8
ios swift uistoryboard xcode-storyboard
Feb 12 '16 at 5:51
source share
3 answers

Although you cannot specify both your own font and the preferred text style through the Storyboard, it is not difficult to programmatically specify the size of the dynamic type for your custom font:

let pointSize = UIFontDescriptor.preferredFontDescriptorWithTextStyle(UIFont‌​TextStyleTitle1).poi‌​ntSize let customFont = UIFont(name: "Chalkboard SE", size: pointSize) 

When you get UIContentSizeCategoryDidChangeNotification , use the same code to update the font of the label.

+16
Feb 12 '16 at 7:55
source share

Try my solution without having to listen to NSNotification.Name.UIContentSizeCategoryDidChange.

Just override traitCollectionDidChange on any UIView or UITableViewCell and update / copy the font using a similar TextStyle.

You can simply test it with any simulated device and accessibility inspector on MacOS.

  override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) let pointSize = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .title1).pointSize titleLabel.font = titleLabel.font.withSize(pointSize) } 
0
May 14 '18 at 16:28
source share

We must add all the extensions of the associated UIFramework, for which we must use the dynamic font size of the custom font in iOS-11 +, for example UILabel, UIButton, UITextField, UITextView. The code is given in quick 4.2. We just need to use these custom classes instead of the native ios classes to see the effect of dynamic fonts in the application.

Here's the Shortcut first:

 class AppLabel: UILabel { override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) if #available(iOS 11.0, *) { self.font = UIFontMetrics.default.scaledFont(for: self.font) self.adjustsFontForContentSizeCategory = true // Fallback on earlier versions } else { // Fallback on earlier versions } } } 

Here is the 2nd UIButton:

 class AppButton: UIButton { override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) if #available(iOS 11.0, *) { self.titleLabel?.font = UIFontMetrics.default.scaledFont(for: self.titleLabel?.font ?? UIFont()) self.titleLabel?.adjustsFontForContentSizeCategory = true } else { // Fallback on earlier versions } } } 

Here's the third UITextField:

 class AppTextField: UITextField { override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) if #available(iOS 11.0, *) { self.font = UIFontMetrics.default.scaledFont(for: self.font ?? UIFont()) self.adjustsFontForContentSizeCategory = true } else { // Fallback on earlier versions } } } 

Last in UITextView:

 class AppTextView: UITextView { override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) if #available(iOS 11.0, *) { self.font = UIFontMetrics.default.scaledFont(for: self.font ?? UIFont()) self.adjustsFontForContentSizeCategory = true } else { // Fallback on earlier versions } } } 
0
Nov 30 '18 at 13:57
source share



All Articles