34

I'm trying to apply a custom font throughout my iOS app. I found that I could use:

 [[UILabel appearance] setFont:[UIFont fontWithName:@"Proxima Nova" size:17.0]];
  • To set the default font and size for all UILabels. However, not all my UILabels share the same font size.

  • In Set a default font for whole iOS app?, someone had the same concern, and was told to set the size parameter to 0.0 to only set the font and not font size.

  • When I tried doing this, all the UILabel text in my app disappeared (because evidently iOS took the 0.0 font size literally).

Any suggestions as to how I can universally set a font but not size? Thanks a lot!

0

7 Answers 7

24
- (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];
        }
    }    
}
3
  • 8
    +1 However, this approach fails if your app is using a UITableView. Labels in the individual cells will not be updated by this process.
    – Fostah
    May 1, 2013 at 19:41
  • 4
    Yes, but so what? Handling UILabels in UITableViewCells whether they're custom or built in, is a matter of one line of text in your table's cellForRowAtIndexPath method. Jun 24, 2013 at 13:38
  • 1
    This might be the best you can do, but the other thing this solution doesn't address is different font styling such as bold or italic when you have a separate family for those fonts, such as "FagoOfficeSans-Bold".
    – d512
    Mar 27, 2014 at 21:04
19

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"];
3
  • 4
    Little info for curious devs: "UIAppearance looks for the UI_APPEARANCE_SELECTOR macro in method signatures. Any method with this annotation can be used with the appearance proxy." http://nshipster.com/uiappearance/
    – Zeb
    Oct 16, 2015 at 11:22
  • How to use this in Swift Appdelegate ?
    – Amr Hossam
    Aug 14, 2017 at 10:23
  • Amazing!! Thanks
    – ΩlostA
    Feb 2, 2018 at 10:53
12

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];
        }
    }
}
6

By writing the category for the Label we can change the fonts of entire app.

@implementation UILabel (CustomeFont)

-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setBackgroundColor:[UIColor clearColor]];
    [self setFont:[UIFont fontWithName:@"Helvetica" size:self.font.pointSize]];
}

@end
1
  • 6
    You are strongly discouraged from overriding class methods. At least there is no way to invoke the original implementation.
    – beryllium
    Dec 18, 2013 at 18:37
5

Swift3 https://gist.github.com/dimohamdy/c47992e14c1a3ee3315c06e2480e121e

you can't set appearance for label font that make me create other value of default font and when i set the new font i get only the name of new font and old size of label and create other font and set this font

    //when you want to set font for all labels in Application
    UILabel.appearance().defaultFont =  UIFont.systemFont(ofSize: 15/*Any Value*/, weight: UIFontWeightThin)

extension UILabel{
    dynamic var defaultFont: UIFont? {
        get { return self.font }
        set {
          //get old size of lable font
          let sizeOfOldFont = self.font.pointSize 
          //get new name  of font
          let fontNameOfNewFont = newValue?.fontName
          self.font = UIFont(name: fontNameOfNewFont!, size: sizeOfOldFont)
        }
    }
     }
1
  • You could improve your answer by explaining how it works.
    – James K
    Sep 28, 2016 at 20:11
3

UIView+DefaultFontAndColor.h

#import <UIKit/UIKit.h>

@interface UIView (DefaultFontAndColor)

-(void)setDefaultFontFamily:(NSString*)fontFamily andSubViews:(BOOL)isSubViews andColor:(UIColor*) color;
@end

UIView+DefaultFontAndColor.m

#import "UIView+DefaultFontAndColor.h"

@implementation UIView (DefaultFontAndColor)

//sets the default font for view classes by default
-(void)setDefaultFontFamily:(NSString*)fontFamily andSubViews:(BOOL)isSubViews andColor:     (UIColor*) color
{
    if ([self isKindOfClass:[UILabel class]])
    {
        UILabel *lbl = (UILabel *)self;
        [lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];

        if( color )
            lbl.textColor = color;
    }
    else if ([self isKindOfClass:[UIButton class]])
    {
        UIButton *btn = (UIButton *)self;
        [btn.titleLabel setFont:[UIFont fontWithName:fontFamily size:[[btn.titleLabel font] pointSize]]];

        if( color )
        {
            btn.tintColor = color;
        }
    }

    if (isSubViews)
    {
        for (UIView *sview in self.subviews)
        {
            [sview setDefaultFontFamily:fontFamily andSubViews:YES andColor:color];
        }
    }
}
@end

@usage: without color:

#import "UIView+DefaultFontAndColor.h"

UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:@"Arial" andSubViews:YES andColor:nil];

@usage: with color:

#import "UIView+DefaultFontAndColor.h"

UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:@"Arial" andSubViews:YES andColor:[UIColor greenColor] ];
3

Raegtime's answer ala Swift...

import UIKit

extension UIViewController {
    func setFontFamilyForView(_ fontFamily: String, view: UIView, andSubviews: Bool) {
        if let label = view as? UILabel {
            label.font = UIFont(name: fontFamily, size: label.font.pointSize)
        }

        if let textView = view as? UITextView {
            textView.font = UIFont(name: fontFamily, size: textView.font!.pointSize)
        }

        if let textField = view as? UITextField {
            textField.font = UIFont(name: fontFamily, size: textField.font!.pointSize)
        }

        if andSubviews {
            for v in view.subviews {
                setFontFamilyForView(fontFamily, view: v, andSubviews: true)
            }
        }
    }
}
2
  • 4
    What's your problem? Can't edit the answer cos it isn't mine, couldn't add that snippet in a comment because of character limit. I found this question even though I'm working in Swift, so obviously others will too. I thought this snippet might be helpful to them. Do you have nothing better to do other than go around bashing people for trying to help others?
    – loglesby
    Feb 25, 2018 at 16:19
  • There is no need to have two same answers in different languages. Even if the answer is not yours, you can edit it if you have enough reputation, or if you don't, add a comment asking for a Swift version. That will help other users more than adding a brand new answer. Adding a new answer should be the last resort, not the first.
    – Sulthan
    Feb 25, 2018 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.