Default Uifont Size and Weight But Also Support Preferredfontfortextstyle

Default Font for UINavigationBar Large Title in iOS 11?

The proper solution for getting the large title font is the following:

Objective-C:

UIFont *font = [UIFont preferredFontForTextStyle: UIFontTextStyleLargeTitle];
NSLog("%@", font);

Swift:

let font = UIFont.preferredFont(forTextStyle: .largeTitle)
print(font)

This will give the correct value even if the user has applied various accessibility and font size changes in the Settings app.

The above will print out the following by default (in a playground):

<UICTFont: 0x7fa865c05390> font-family: ".SFUIDisplay"; font-weight: normal; font-style: normal; font-size: 34.00pt

Note that the use of .largeTitle in the code above must be called only with iOS 11. If your app supports iOS 10 or earlier, you must wrap that code in a proper use of @available.

Default UITableViewCellStyleSubtitle font size?

The actual font size depends on the user's settings in Settings -> General -> TextSize. Normally, you shouldn't use a fixed font size, but should use something like:

[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]

obviously depending on what you need. Anyway, if you create a UITableViewCell with style UITableViewCellStyleSubtitle, then the font of cell.text is the same object as

[UIFont preferredFontForTextStyle: UIFontTextStyleBody]

and the font of cell.detailTextLabel is the same object as

[UIFont preferredFontForTextStyle: UIFontTextStyleCaption1]. 

You get fonts from largest to smallest using the constants ending in "Body", "Subheadline", "Footnote", "Caption1", "Caption2" so you know what to use if you want slightly smaller or bigger text. "Headline" is same size as "Body" but bold.

It's probably best to just create a cell at runtime and get the fonts from it.

How to use a custom font with dynamic text sizes in iOS7

Behind the scenes of that API, apple has some sort of lookup table that returns a specific font family, size, and sometimes symbolic traits (like bold) that (e.g. UIFontTextStyleHeadline) and the user's preferred text size. The latter is a string pulled off of the sharedApplication like this:

[UIApplication sharedApplication].preferredContentSizeCategory;

(I logged out all the default sizes/fonts/traits for Helvetica-Neue for the various dynamic text sizes). We've since added handling for the accessibility sizes, which is important.

So all you really have to do is build a similar lookup table. Our designer created a simple spreadsheet for me:

Font size lookup table

Notice that we added a couple styles (caption 3 & 4) to have 8 instead of 6 to choose from.

Then you'll want to put it someplace convenient, like a category on UIFontDescriptor. You'll want your method to return a UIFontDescriptor like Apple's API, so that it's still easy to adjust with symbolic traits, etc.

My category looks like this:

UIFontDescriptor+AvenirNext.h

#import <UIKit/UIKit.h>

extern NSString *const ANUIFontTextStyleCaption3;

@interface UIFontDescriptor (AvenirNext)

+(UIFontDescriptor *)preferredAvenirNextFontDescriptorWithTextStyle:(NSString *)style;

@end

UIFontDescriptor+AvenirNext.m

#import "UIFontDescriptor+AvenirNext.h"

NSString *const ANUIFontTextStyleCaption3 = @"ANUIFontTextStyleCaption3";
NSString *const ANUIFontTextStyleCaption4 = @"ANUIFontTextStyleCaption4";

@implementation UIFontDescriptor (AvenirNext)
+(UIFontDescriptor *)preferredAvenirNextFontDescriptorWithTextStyle:(NSString *)style {
static dispatch_once_t onceToken;
static NSDictionary *fontSizeTable;
dispatch_once(&onceToken, ^{
fontSizeTable = @{
UIFontTextStyleHeadline: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @26,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @25,
UIContentSizeCategoryAccessibilityExtraLarge: @24,
UIContentSizeCategoryAccessibilityLarge: @24,
UIContentSizeCategoryAccessibilityMedium: @23,
UIContentSizeCategoryExtraExtraExtraLarge: @23,
UIContentSizeCategoryExtraExtraLarge: @22,
UIContentSizeCategoryExtraLarge: @21,
UIContentSizeCategoryLarge: @20,
UIContentSizeCategoryMedium: @19,
UIContentSizeCategorySmall: @18,
UIContentSizeCategoryExtraSmall: @17,},

UIFontTextStyleSubheadline: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @24,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @23,
UIContentSizeCategoryAccessibilityExtraLarge: @22,
UIContentSizeCategoryAccessibilityLarge: @22,
UIContentSizeCategoryAccessibilityMedium: @21,
UIContentSizeCategoryExtraExtraExtraLarge: @21,
UIContentSizeCategoryExtraExtraLarge: @20,
UIContentSizeCategoryExtraLarge: @19,
UIContentSizeCategoryLarge: @18,
UIContentSizeCategoryMedium: @17,
UIContentSizeCategorySmall: @16,
UIContentSizeCategoryExtraSmall: @15,},

UIFontTextStyleBody: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @21,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @20,
UIContentSizeCategoryAccessibilityExtraLarge: @19,
UIContentSizeCategoryAccessibilityLarge: @19,
UIContentSizeCategoryAccessibilityMedium: @18,
UIContentSizeCategoryExtraExtraExtraLarge: @18,
UIContentSizeCategoryExtraExtraLarge: @17,
UIContentSizeCategoryExtraLarge: @16,
UIContentSizeCategoryLarge: @15,
UIContentSizeCategoryMedium: @14,
UIContentSizeCategorySmall: @13,
UIContentSizeCategoryExtraSmall: @12,},

UIFontTextStyleCaption1: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @19,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @18,
UIContentSizeCategoryAccessibilityExtraLarge: @17,
UIContentSizeCategoryAccessibilityLarge: @17,
UIContentSizeCategoryAccessibilityMedium: @16,
UIContentSizeCategoryExtraExtraExtraLarge: @16,
UIContentSizeCategoryExtraExtraLarge: @16,
UIContentSizeCategoryExtraLarge: @15,
UIContentSizeCategoryLarge: @14,
UIContentSizeCategoryMedium: @13,
UIContentSizeCategorySmall: @12,
UIContentSizeCategoryExtraSmall: @12,},

UIFontTextStyleCaption2: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @18,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @17,
UIContentSizeCategoryAccessibilityExtraLarge: @16,
UIContentSizeCategoryAccessibilityLarge: @16,
UIContentSizeCategoryAccessibilityMedium: @15,
UIContentSizeCategoryExtraExtraExtraLarge: @15,
UIContentSizeCategoryExtraExtraLarge: @14,
UIContentSizeCategoryExtraLarge: @14,
UIContentSizeCategoryLarge: @13,
UIContentSizeCategoryMedium: @12,
UIContentSizeCategorySmall: @12,
UIContentSizeCategoryExtraSmall: @11,},

ANUIFontTextStyleCaption3: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @17,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @16,
UIContentSizeCategoryAccessibilityExtraLarge: @15,
UIContentSizeCategoryAccessibilityLarge: @15,
UIContentSizeCategoryAccessibilityMedium: @14,
UIContentSizeCategoryExtraExtraExtraLarge: @14,
UIContentSizeCategoryExtraExtraLarge: @13,
UIContentSizeCategoryExtraLarge: @12,
UIContentSizeCategoryLarge: @12,
UIContentSizeCategoryMedium: @12,
UIContentSizeCategorySmall: @11,
UIContentSizeCategoryExtraSmall: @10,},

UIFontTextStyleFootnote: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @16,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @15,
UIContentSizeCategoryAccessibilityExtraLarge: @14,
UIContentSizeCategoryAccessibilityLarge: @14,
UIContentSizeCategoryAccessibilityMedium: @13,
UIContentSizeCategoryExtraExtraExtraLarge: @13,
UIContentSizeCategoryExtraExtraLarge: @12,
UIContentSizeCategoryExtraLarge: @12,
UIContentSizeCategoryLarge: @11,
UIContentSizeCategoryMedium: @11,
UIContentSizeCategorySmall: @10,
UIContentSizeCategoryExtraSmall: @10,},

ANUIFontTextStyleCaption4: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @15,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @14,
UIContentSizeCategoryAccessibilityExtraLarge: @13,
UIContentSizeCategoryAccessibilityLarge: @13,
UIContentSizeCategoryAccessibilityMedium: @12,
UIContentSizeCategoryExtraExtraExtraLarge: @12,
UIContentSizeCategoryExtraExtraLarge: @11,
UIContentSizeCategoryExtraLarge: @11,
UIContentSizeCategoryLarge: @10,
UIContentSizeCategoryMedium: @10,
UIContentSizeCategorySmall: @9,
UIContentSizeCategoryExtraSmall: @9,},
};
});


NSString *contentSize = [UIApplication sharedApplication].preferredContentSizeCategory;
return [UIFontDescriptor fontDescriptorWithName:[self preferredFontName] size:((NSNumber *)fontSizeTable[style][contentSize]).floatValue];
}
+(UIFontDescriptor *)preferredAvenirNextDemiBoldFontDescriptorWithTextStyle:(NSString *)style {
return [[self preferredAvenirNextFontDescriptorWithTextStyle:style] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
}

+(UIFontDescriptor *)preferredAvenirNextBoldFontDescriptorWithTextStyle:(NSString *)style {
return [UIFontDescriptor fontDescriptorWithName:[self preferredBoldFontName] size:[self preferredAvenirNextFontDescriptorWithTextStyle:style].pointSize];
}

+(NSString *)preferredFontName {
return @"AvenirNext-Medium";
}
+(NSString *)preferredBoldFontName {
return @"AvenirNext-Bold";
}

@end

We chose to use the same base font AvenirNext-Medium, and then bold and such via symbolic traits, but you could get crazy and specify different weight variants on your font as part of your lookup table as well if you wanted, like AvenirNext-ExtraBold.

That's all there is to it! We use it like this:

[UIFont fontWithDescriptor:[UIFontDescriptor preferredAvenirNextFontDescriptorWithTextStyle:UIFontTextStyleHeadline] size: 0]

UIFontWeightTrait and UIFontDescriptorFamilyAttribute Ignored when creating UIFont from UIFontDescriptor

I ran into the same issue, and the documentation was not much help. Eventually I figured out that using the family attribute combined with the face attribute worked:

 UIFontDescriptor* desc = [UIFontDescriptor fontDescriptorWithFontAttributes:
@{
UIFontDescriptorFamilyAttribute: @"Helvetica Neue",
UIFontDescriptorFaceAttribute: @"Light"
}
];

Possible to detect Bold Text setting in Settings Accessibility?

As of iOS 8, it is possible to detect whether the user has enabled Bold Text in Settings using UIAccessibility.isBoldTextEnabled (docs) and UIAccessibility.boldTextStatusDidChangeNotification (docs).

For apps that also require iOS 7 support, I’ve written an elegant one-liner that works on iOS 7 & 8 with Helvetica Neue and even on iOS 9 with the San Francisco typeface, based on the fact that standard-weight fonts are commonly referred to as the “Regular” weight, and that body text uses this weight for readability:

Objective-C:

BOOL hasBoldText = ![[UIFont preferredFontForTextStyle:UIFontTextStyleBody].fontName hasSuffix:@"-Regular"];

Swift:

let hasBoldText = !UIFont.preferredFontForTextStyle(UIFontTextStyleBody).fontName.hasSuffix("-Regular")


Related Topics



Leave a reply



Submit