UILabel, UITextField and UITextView should have a textStyle property

Originator:michaelhochs
Number:rdar://30049158 Date Originated:17-Jan-2017 09:43 AM
Status:3rd party to Resolve Resolved:
Product:iOS Product Version:10.2
Classification:Enhancement Reproducible:Always
 
Dynamic font on iOS is great, but it is still a lot of work to fully support it. One of the biggest and most annoying tasks when doing so is to listen to the `UIContentSizeCategoryDidChangeNotification` notification and update all the fonts in all the labels. In order to do this you need to have a reference to each label on the screen, you need to know what text style the given label should have and you need to update them all. If you are using interface builder this is particularly annoying as it kind of breaks with the pattern to have layout related things setup in interface builder, as you need to check for that notification and assign a new font in interface builder. But even if you don’t use interface builder, the fact that you need to reference every single label, text field and text view on the screen somewhere blows up the code and assigning the new fonts essentially just is a lot of boiler plate.

So some people, including myself, write a label that has a textStyle property and then change all their labels to be of type textStyle. This label does the following: Instead of assigning it a font, you assign it a `UIFontTextStyle` and then the label monitors the `UIContentSizeCategoryDidChangeNotification` itself and updates its font accordingly.

This is something that should be build into UIKit. It would be very helpful to have this available on every label without the need to change the base class of every label. Also Apple could do a far better support for interface builder on this than any other developer could do.

I am fairly sure that this would increase the amount of apps that properly support dynamic font a lot as it would dramatically decrease development time for this feature to almost no time as the only thing you need to do is instead of configuring a font you just set the text style.

Comments

Engineering has provided the following information regarding this issue:

UILabel, UITextField and UITextView already support this since iOS 10: they all adopt the UIContentSizeCategoryAdjusting protcol, which has:

@property (nonatomic) BOOL adjustsFontForContentSizeCategory;

which defaults to NO.

The header says:

“Indicates whether the corresponding element should automatically update its font when the device’s UIContentSizeCategory is changed. For this property to take effect, the element’s font must be a font vended using +preferredFontForTextStyle: or +preferredFontForTextStyle:compatibleWithTraitCollection: with a valid UIFontTextStyle.”

So: - set . adjustsFontForContentSizeCategory = YES - set .font to a preferred font (+[UIFont preferredFontForTextStyle:] +[UIFont preferredFontForTextStyle: compatibleWithTraitCollection:]). This also works for different text style fonts in attributed strings.

By michaelhochs at Feb. 3, 2017, 8:05 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!