Change default font to custom fonts in whole app

前端 未结 6 1979
猫巷女王i
猫巷女王i 2020-12-30 16:54

I want to keep custom fonts in iOS app. Currently, I\'m using iOS default fonts. Is there any possible way for the same?

How do I include the custom font so that I c

相关标签:
6条回答
  • 2020-12-30 17:00
    1. Copy your font file into resources

    2. Add a key to your Info.plist file called UIAppFonts. ("Fonts provided by application")

    3. Make this key an array

    4. For each font you have, enter the full name of your font file (including the extension) as items to the UIAppFonts array

    5. Save Info.plist

    6. Now in your application you can simply call following method to get the custom font to use with your UILabel and UITextView, etc…

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

    Check which 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 font for whole application in UILabel :

    Add code in AppDelegate.m

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

    Change font in one viewConroller same as font size set in 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];
            }
        }    
    }
    
    0 讨论(0)
  • 2020-12-30 17:01

    Ok, from all the other answers you are now very well familier that how to add a custom font (other than iOS default font) into app.

    So now what? One thing is still remaining, I'm not sure if you want to use the same size custom font everywhere. If so,

    I'm using this way to have custom font in my app.

    If your app will have a single custom font then you can use it like this way for entire app.

    #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've added these macros globally (so can access it from anywhere)

    and you can use it like this, yourLabel.font = appFontRegular(12); or yourTextField.font = appFontBold(14); this helps you in following situations,

    1. Whenever you needs to change the custom font, you only need to change font name in macros.
    2. Your code will looks clean, no need write [UIFont fontWithName: size:]; everywhere.

    If your app will have more than one custom font then you can use it like this way for entire app.

    then also you can define different macros like this,

    Then above macro will be the same (as this is the maximum used fonts in app), 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]
    
    0 讨论(0)
  • 2020-12-30 17:08

    Add your custom font into your project , i.e. Dragged the font file(CALIBRIZ_0.TTF) into XCode project.

    Edit Info.plist: Add a new entry with the key "Fonts provided by application".

    For each of your files, add the file name to this array

    Now set font to your label

    yourLabel.font = [UIFont fontWithName:@"Calibri" size:15];
    
    0 讨论(0)
  • 2020-12-30 17:13

    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 font in whole app even on UIButton, UILabel, inside UITableViewCell, UICollectionViewCell, UIView, UIContainerView, or anywhere in application without changing the font point size.

    This is memory efficient approach rather than enumerating all views in your UIViewController.

    0 讨论(0)
  • 2020-12-30 17:16

    1- Drag & Drop Font file .ttf to your project

    2- Verify the font is in the project.

    By selecting the font, and verifying “Target Membership” in the Utilities area.

    enter image description here

    3- Add info in plist Plist file image

    4- In 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 will help

    0 讨论(0)
  • 2020-12-30 17:21

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

    for example:

    #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've created the category, you will need to import this class for all the file, or you can add it to '.pch' file for your project.

    same solution for buttons / textview ..

    0 讨论(0)
提交回复
热议问题