Reputation: 75276
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.
Upvotes: 6
Views: 8461
Reputation: 1172
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")
}
Upvotes: 16
Reputation: 83
Kind of late to answer this question, but just wanted to add this for completeness.
Here is the documentation on preferredContentSizeCategory
And here is the documentation for defined content size constants
https://developer.apple.com/documentation/uikit/uicontentsizecategory?language=objc
Example usage:
- (void) viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didChangePreferredContentSize:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
[self didChangePreferredContentSize: nil];
}
- (void) didChangePreferredContentSize: (NSNotification *) notification {
CGFloat pointSize = [UIFontDescriptor preferredFontDescriptorWithTextStyle: UIFontTextStyleCaption2].pointSize;
CGFloat pointSizeForRegularFonts = [UIFontDescriptor preferredFontDescriptorWithTextStyle: UIFontTextStyleSubheadline].pointSize;
// NSString *newFontSize = notification.userInfo[UIContentSizeCategoryNewValueKey];
NSString *newFontSize = [UIApplication sharedApplication].preferredContentSizeCategory;
if ([newFontSize isEqualToString: UIContentSizeCategoryExtraSmall] ||
[newFontSize isEqualToString: UIContentSizeCategorySmall] ||
[newFontSize isEqualToString: UIContentSizeCategoryMedium] ||
[newFontSize isEqualToString: UIContentSizeCategoryLarge] ||
[newFontSize isEqualToString: UIContentSizeCategoryAccessibilityMedium] ||
[newFontSize isEqualToString: UIContentSizeCategoryAccessibilityLarge]) {
pointSize = pointSizeForRegularFonts;
}
[self.descriptionLabel setFont: [UIFont systemFontOfSize: pointSize]];
[self.button.titleLabel setFont: [UIFont systemFontOfSize: pointSize]];
}
Upvotes: 0
Reputation: 75276
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;
}
Upvotes: 6
Reputation: 179422
Actually, it seems that it is that method, based on this page:
To take advantage of these features, be prepared to respond to the notifications that get sent when users change the text size setting. Then, use the
UIFont
methodpreferredFontForTextStyle
to get the new font to use in your UI. iOS 7 optimizes fonts that are specified by this method for maximum legibility at every size. To learn more about text styles and using fonts in your app, see “Text Styles” in Text Programming Guide for iOS.
Additionally, the documentation for +[UIFont preferredFontForTextStyle:]
explicitly says that it returns "an instance of the font associated with the text style and scaled appropriately for the user's selected content size category." In other words, the returned UIFont
is already scaled to the user's preferences.
You can obtain the font size from the UIFont
by using -[UIFont pointSize]
.
Upvotes: 1