How to set a custom font for entire iOS app without specifying size How to set a custom font for entire iOS app without specifying size ios ios

How to set a custom font for entire iOS app without specifying size


- (void)viewDidLoad{    [super viewDidLoad];    [self setFontFamily:@"FagoOfficeSans-Regular" forView:self.view andSubViews:YES];}-(void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews{    if ([view isKindOfClass:[UILabel class]])    {        UILabel *lbl = (UILabel *)view;        [lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];    }    if (isSubViews)    {        for (UIView *sview in view.subviews)        {            [self setFontFamily:fontFamily forView:sview andSubViews:YES];        }    }    }


Here is a solution in Objective-C, put this category anywhere you want to change UILabel Apperance without setting UILabel FontSize:

@implementation UILabel (SubstituteFontName)- (void)setSubstituteFontName:(NSString *)name UI_APPEARANCE_SELECTOR {    self.font = [UIFont fontWithName:name size:self.font.pointSize];}@end

Then, you can change the Apperance with:

[[UILabel appearance] setSubstituteFontName:@"SourceSansPro-Light"];


I've used the accepted answer in my project, but needed a more generic function, so it'll change the font to every one possible, also I've chose to set a mapping between some stock fonts to our custom fonts, so they'll be accessible via storybuilder and xib files as well.

+ (void)setupFontsForView:(UIView *)view andSubViews:(BOOL)isSubViews{    if ([view respondsToSelector:@selector(setFont:)] && [view respondsToSelector:@selector(font)]) {        id      viewObj = view;        UIFont  *font   = [viewObj font];        if ([font.fontName isEqualToString:@"AcademyEngravedLetPlain"]) {            [viewObj setFont:[UIFont fontWithName:PRIMARY_FONT size:font.pointSize]];        } else if ([font.fontName hasPrefix:@"AmericanTypewriter"]) {            [viewObj setFont:[UIFont fontWithName:SECONDARY_FONT size:font.pointSize]];        }    }    if (isSubViews) {        for (UIView *sview in view.subviews) {            [self setupFontsForView:sview andSubViews:YES];        }    }}