Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What method do you call to get the user's preferred text size?

Tags:

ios

iOS has a method that you call that will tell you a user's selected Dynamic Type text size (which they set in Settings > General > Text Size and has 7 possible values from small to large).

There is an API call that you can make, that returns one of seven string values that describe these seven options, but I can't seem to find it. I know it exists as I've used it before, but I can't find it anywhere.

Note: I am not talking about this method:

[UIFont preferredFontForTextStyle:UIFontTextStyleBody]

which returns a UIFont.

like image 525
MusiGenesis Avatar asked Dec 06 '22 03:12

MusiGenesis


2 Answers

Swift 4:

This is what I am using to determine the current font category in Swift.

 let fontCategory = UIApplication.shared.preferredContentSizeCategory

    switch fontCategory {
    case UIContentSizeCategory.accessibilityExtraExtraExtraLarge:
        print("A_XXXL")
    case UIContentSizeCategory.accessibilityExtraExtraLarge:
        print("A_XXL")
    case UIContentSizeCategory.accessibilityExtraLarge:
        print("A_XL")
    case UIContentSizeCategory.accessibilityLarge:
        print("A_L")
    case UIContentSizeCategory.accessibilityMedium:
        print("A_M")
    case UIContentSizeCategory.extraExtraExtraLarge:
        print("XXXL")
    case UIContentSizeCategory.extraExtraLarge:
        print("XXL")
    case UIContentSizeCategory.extraLarge:
        print("XL")
    case UIContentSizeCategory.large:
        print("L")
    case UIContentSizeCategory.medium:
        print("M")
    case UIContentSizeCategory.small:
            print("S")
    case UIContentSizeCategory.extraSmall:
        print("XS")
    case UIContentSizeCategory.unspecified:
        print("Unspecified")
    default:
        print("Unknown")
    }
like image 75
Dave Levy Avatar answered Jan 01 '23 06:01

Dave Levy


Found it, it's [[UIApplication sharedApplication] preferredContentSizeCategory]. Here's some code you can use to get this property and turn it into whatever scale of font sizes you like:

CGFloat fontSize = 17;
NSString *textSize = [[UIApplication sharedApplication] 
    preferredContentSizeCategory];
if ([textSize isEqualToString:@"UICTContentSizeCategoryXS"]) {
    fontSize = 10;
} else if ([textSize isEqualToString:@"UICTContentSizeCategoryS"]) {
    fontSize = 13;
} else if ([textSize isEqualToString:@"UICTContentSizeCategoryM"]) {
    fontSize = 15;
} else if ([textSize isEqualToString:@"UICTContentSizeCategoryL"]) {
    // "Normal" or middle size - use designed font sizes
    fontSize = 17;
} else if ([textSize isEqualToString:@"UICTContentSizeCategoryXL"]) {
    fontSize = 24;
} else if ([textSize isEqualToString:@"UICTContentSizeCategoryXXL"]) {
    fontSize = 32;
} else if ([textSize isEqualToString:@"UICTContentSizeCategoryXXXL"]) {
    fontSize = 48;
}
like image 32
MusiGenesis Avatar answered Jan 01 '23 05:01

MusiGenesis