Created
August 27, 2014 17:32
-
-
Save ahknight/6ee0708db98366eaab97 to your computer and use it in GitHub Desktop.
Using custom fonts with dynamic type without jumping through a thousand hoops.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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