Make Content Size Category and Text Style Constants Enumerable on iOS

Originator:greg
Number:rdar://16535024 Date Originated:04/06/2014
Status:Open Resolved:
Product:iOS SDK Product Version:7.1
Classification:Enhancement Reproducible:n/a
 
Summary:
Instead of having Content Size Category Constants (including Accessibility contents and UIApplication's preferredContentSizeCategory) as NSStrings, make them enumerations. This would allow for cleaner/faster code when supporting DynamicType with custom (non system-provided) fonts.

Steps to Reproduce:
Suppose we're using a custom font in an app and we're supporting DynamicType. With a system font, this means we'd set UILabel fonts with UIFont's preferredFontForTextStyle. With a custom font, we have to essentially mimic preferredFontForTextStyle, but return our custom font in the appropriate size. We might make a UIFont category message called preferredCustomFontForTextStyle, whose body looks like:

CGFloat fontSize;
if ([style isEqualToString:UIFontTextStyleHeadline]) {
		if ([preferredContentSizeCategory isEqualToString:UIContentSizeCategoryExtraSmall])
			fontSize = 12.0;
		else if ([preferredContentSizeCategory isEqualToString:UIContentSizeCategorySmall])
			fontSize = 14.0;
		else if ([preferredContentSizeCategory isEqualToString:UIContentSizeCategoryMedium])
			fontSize = 16.0;
		else if ([preferredContentSizeCategory isEqualToString:UIContentSizeCategoryLarge])
			fontSize = 18.0;
		else if ([preferredContentSizeCategory isEqualToString:UIContentSizeCategoryExtraLarge])
			fontSize = 20.0;
		else if ([preferredContentSizeCategory isEqualToString:UIContentSizeCategoryExtraExtraLarge])
			fontSize = 22.0;
		else if ([preferredContentSizeCategory isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge])
			fontSize = 24.0;
...

return ([UIFont fontWithName:@"customFont" size:fontSize]);

We need to repeat this for each and every Content Size Category, Accessibility Content Size Category, and Text Style. In the worst case, we have to enumerate and isEqualToString every single constant. If the constants were enumerable, the code would still be a little bit ugly, but it would run much faster.

CGFloat fontSize;
switch (textStyle) {
case UIFontTextStyleHeadline:
        switch (preferredContentSizeCategory) {
        case UIContentSizeCategoryLarge:
                fontSize = 18.0;
                break;
        ...
        default:
                fontSize = 12.0;
                break;
        }
case ...
        ....
}
return ([UIFont fontWithName:@"customFont" size:18.0]);

Expected Results:
Faster slightly-less-ugly-but-still-big code.

Actual Results:
Slow ugly code.

Comments


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!