Created
December 25, 2014 09:42
-
-
Save andrey-str/7df571d5e92acbbe226f to your computer and use it in GitHub Desktop.
UIFont + Custon font with Dynamic Sizes
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
#import <UIKit/UIKit.h> | |
extern NSString* const UIFontTextStyleBoldBody; | |
@interface UIFont (ContentSize) | |
+ (UIFont*)preferredSystemFontForTextStyle:(NSString*)textStyle; | |
@end |
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
#import "UIFont+ContentSize.h" | |
NSString* const UIFontTextStyleBoldBody = @"UIFontTextStyleBoldBody"; | |
@implementation UIFont (ContentSize) | |
+ (UIFont *)preferredSystemFontForTextStyle:(NSString *)textStyle | |
{ | |
// choose the font size | |
CGFloat fontSize = 11.f; | |
NSString *contentSize = [UIApplication sharedApplication].preferredContentSizeCategory; | |
static dispatch_once_t onceToken; | |
static NSDictionary *fontSizeOffsetDictionary; | |
dispatch_once(&onceToken, ^{ | |
fontSizeOffsetDictionary = @{ UIContentSizeCategoryExtraSmall : @{ UIFontTextStyleBody : @(-2), | |
UIFontTextStyleBoldBody: @(-2), | |
UIFontTextStyleHeadline : @(-2), | |
UIFontTextStyleSubheadline : @(-4), | |
UIFontTextStyleCaption1 : @(-5), | |
UIFontTextStyleCaption2 : @(-5), | |
UIFontTextStyleFootnote : @(-4) }, | |
UIContentSizeCategorySmall : @{ UIFontTextStyleBody : @(-1), | |
UIFontTextStyleBoldBody: @(-1), | |
UIFontTextStyleHeadline : @(-1), | |
UIFontTextStyleSubheadline : @(-3), | |
UIFontTextStyleCaption1 : @(-5), | |
UIFontTextStyleCaption2 : @(-5), | |
UIFontTextStyleFootnote : @(-4) }, | |
UIContentSizeCategoryMedium : @{ UIFontTextStyleBody : @(-2), | |
UIFontTextStyleBoldBody: @(-2), | |
UIFontTextStyleHeadline : @(-2), | |
UIFontTextStyleSubheadline : @(-4), | |
UIFontTextStyleCaption1 : @(-7), | |
UIFontTextStyleCaption2 : @(-7), | |
UIFontTextStyleFootnote : @(-6) }, | |
UIContentSizeCategoryLarge : @{ UIFontTextStyleBody : @(0), | |
UIFontTextStyleBoldBody: @(0), | |
UIFontTextStyleHeadline : @(0), | |
UIFontTextStyleSubheadline : @(-2), | |
UIFontTextStyleCaption1 : @(-5), | |
UIFontTextStyleCaption2 : @(-6), | |
UIFontTextStyleFootnote : @(-4) }, | |
UIContentSizeCategoryExtraLarge : @{ UIFontTextStyleBody : @(2), | |
UIFontTextStyleBoldBody: @(2), | |
UIFontTextStyleHeadline : @(2), | |
UIFontTextStyleSubheadline : @(0), | |
UIFontTextStyleCaption1 : @(-3), | |
UIFontTextStyleCaption2 : @(-4), | |
UIFontTextStyleFootnote : @(-2) }, | |
UIContentSizeCategoryExtraExtraLarge : @{ UIFontTextStyleBody : @(3), | |
UIFontTextStyleBoldBody: @(3), | |
UIFontTextStyleHeadline : @(3), | |
UIFontTextStyleSubheadline : @(1), | |
UIFontTextStyleCaption1 : @(-2), | |
UIFontTextStyleCaption2 : @(-3), | |
UIFontTextStyleFootnote : @(-1) }, | |
UIContentSizeCategoryExtraExtraExtraLarge : @{ UIFontTextStyleBody : @(4), | |
UIFontTextStyleBoldBody: @(4), | |
UIFontTextStyleHeadline : @(4), | |
UIFontTextStyleSubheadline : @(2), | |
UIFontTextStyleCaption1 : @(-1), | |
UIFontTextStyleCaption2 : @(-2), | |
UIFontTextStyleFootnote : @(0) } | |
}; | |
}); | |
// adjust the default font size based on what the User has set in Settings | |
CGFloat fontSizeOffset = [fontSizeOffsetDictionary[contentSize][textStyle] doubleValue]; | |
fontSize += fontSizeOffset; | |
// choose the font weight | |
if ([textStyle isEqualToString:UIFontTextStyleHeadline] || | |
[textStyle isEqualToString:UIFontTextStyleSubheadline] || [textStyle isEqualToString: UIFontTextStyleBoldBody]) { | |
return [UIFont boldSystemFontOfSize:fontSize]; | |
} else { | |
return [UIFont systemFontOfSize:fontSize]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment