Skip to content

Instantly share code, notes, and snippets.

@ahknight
Created August 27, 2014 17:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahknight/6ee0708db98366eaab97 to your computer and use it in GitHub Desktop.
Save ahknight/6ee0708db98366eaab97 to your computer and use it in GitHub Desktop.
Using custom fonts with dynamic type without jumping through a thousand hoops.
@interface UIFont (Sanity)
+(UIFont*)preferredFontForTextStyle:(NSString *)style withFontFamily:(NSString*)family;
@end
@implementation UIFont (Sanity)
+(UIFont*)preferredFontForTextStyle:(NSString*)style withFontFamily:(NSString*)family
{
UIFont *font = nil;
UIFontDescriptor *descriptor = nil;
// Font family and size
descriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:style];
descriptor = [UIFontDescriptor fontDescriptorWithName:family size:descriptor.pointSize];
// Font traits for special styles.
if ([style isEqual:UIFontTextStyleHeadline]) {
descriptor = [descriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
} else if ([@[UIFontTextStyleCaption1, UIFontTextStyleCaption2] containsObject:style]) {
descriptor = [descriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];
}
font = [UIFont fontWithDescriptor:descriptor size:descriptor.pointSize];
return font;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment