Change the default font for custom fonts in the whole application

I want to store custom fonts in an iOS app. I am currently using the default iOS fonts. Is there any possible way to do this?

How do I enable a custom font so I can use it?

+3


source to share


6 answers


Make a category:

#import "UILabel+Helper.h"

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

      

Then in AppDelegate.m:



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

      

This will change the font in the application as a whole even UIButton

, UILabel

inside UITableViewCell

, UICollectionViewCell

, UIView

, UIContainerView

or anywhere in the application, without changing the font size.

This is a memory efficient approach, not listing all the views in yours UIViewController

.

+14


source


  • Copy the font file to resources

  • Add the key to the Info.plist file named UIAppFonts

    . ("Fonts provided on request")

  • Make this key an array

  • For each font you have, enter the full name of the font file (including the extension) as array elements UIAppFonts

  • Save Info.plist

  • Now, in your application you can simply call the following method to get your own font that will be used with your UILabel

    and UITextView

    etc.

[UIFont fontWithName:@"CustomFontName" size:15];

Check what fonts are available in your resources:

func allFonts(){
   for family in UIFont.familyNames(){
       println(family)
       for name in UIFont.fontNamesForFamilyName(family.description)
       {
           println("  \(name)")
       }
   }
}

      

Change the font for the whole application in UILabel

:



Add code to AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     ...
     [[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:17]];
     ...
}

      

Change the font in one viewConroller

in the same way as the font size set in the view design:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setFontFamily:@"YourFontName" 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];
        }
    }    
}

      

+9


source


Ok, from all the other answers, you are now very familiar with how to add your own font to your app (other than the default iOS font).

So now? One thing remains, I'm not sure if you want to use your own font size all over the world. If yes,

I use this way to have my own font in my application.

If your application will have one custom font, you can use it that way for the entire application.

#define appFontBold(x) [UIFont fontWithName:@"CustomFont-Bold" size:x]
#define appFontLight(x) [UIFont fontWithName:@"CustonFont-Light" size:x]
#define appFontRegular(x) [UIFont fontWithName:@"CustonFont-Regular" size:x]

      

I added these macros globally (so you can access it from anywhere)

and you can use it like this: yourLabel.font = appFontRegular(12);

or yourTextField.font = appFontBold(14);

, it will help you in the following situations:

  • Whenever you need to change your own font, you only need to change the font name in macros.
  • Your code will look clean, no need to write [UIFont fontWithName: size:]

    ; worldwide.

If your application will have more than one custom font, you can use it that way for the entire application.

then you can also define different macros like

Then the above macro will be the same (as used in the max font fonts) for other fonts:

#define appFontBoldOtherFont(x) [UIFont fontWithName:@"CustomOtherFont-Bold" size:x]
#define appFontLightOtherFont(x) [UIFont fontWithName:@"CustonOtherFont-Light" size:x]
#define appFontRegularOtherFont(x) [UIFont fontWithName:@"CustonOtherFont-Regular" size:x]

      

+1


source


1- Drag and drop file .ttf file into your project

2- Check the font in the project.

By choosing a font and checking the Target Membership in the Utilities area.

enter image description here

3- Add information to plist Plist file image

4- In the delegate add this code

[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:11.0]];

[[UITextField appearance] setFont:[UIFont fontWithName:@"YourFontName" size:11.0]];

[[UITextView appearance] setFont:[UIFont fontWithName:@"YourFontName" size:11.0]];

      

I hope this helps

0


source


Add your own font to your project i.e. drag the font file (CALIBRIZ_0.TTF) into your XCode project.

Edit Info.plist

: Add a new entry with the key " Fonts provided by application

".

For each of your files, add the filename to this array

Now set the font to label

yourLabel.font = [UIFont fontWithName:@"Calibri" size:15];

      

-1


source


If you want to change all fonts for all UILabels in one place, you need to create a UILabel category.

eg:

#import <UIKit/UIKit.h>

@interface UILabel (AppFont)

@end


@implementation UILabel (AppFont)

-(void)awakeFromNib
{
    self.font = [UIFont fontWithName:@"yourCustomFont" size:self.font.pointSize];
}


@end

      

after you have created the category, you will need to import this class for the whole file, or you can add it to the .pch file for your project.

same solution for buttons / textview.

-2


source







All Articles