简体   繁体   English

在整个应用中将默认字体更改为自定义字体

[英]Change default font to custom fonts in whole app

I want to keep custom fonts in iOS app. 我想在iOS应用程序中保留自定义字体。 Currently, I'm using iOS default fonts. 目前,我使用的是iOS默认字体。 Is there any possible way for the same? 有没有可能的方法呢?

How do I include the custom font so that I can use it? 如何包含自定义字体以便我可以使用它?

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: 然后在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. 这将改变整个应用程序中的字体,即使在UIButtonUILabelUITableViewCellUICollectionViewCellUIViewUIContainerView或应用程序中的任何位置,也不会更改字体点大小。

This is memory efficient approach rather than enumerating all views in your UIViewController . 这是内存有效的方法,而不是枚举UIViewController所有视图。

  1. Copy your font file into resources 将您的字体文件复制到资源中

  2. Add a key to your Info.plist file called UIAppFonts . 在Info.plist文件中添加一个名为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 对于您拥有的每种字体,请输入字体文件的全名(包括扩展名)作为UIAppFonts数组的项目

  5. Save Info.plist 保存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… 现在在您的应用程序中,您只需调用以下方法即可获得与UILabelUITextView等一起使用的自定义字体...

[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 : UILabel更改整个应用程序的UILabel

Add code in AppDelegate.m 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: 在一个viewConroller更改字体与在视图设计中设置的字体大小相同:

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

1- Drag & Drop Font file .ttf to your project 1-将字体文件.ttf拖放到项目中

2- Verify the font is in the project. 2-验证字体是否在项目中。

By selecting the font, and verifying “Target Membership” in the Utilities area. 通过选择字体,并在“实用工具”区域中验证“目标成员资格”。

在此输入图像描述

3- Add info in plist 3-在plist中添加信息 Plist文件图像

4- In delegate add this code 4-在委托中添加此代码

[[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 我希望这个能帮上忙

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. 好的,从所有其他答案中,您现在非常熟悉如何在应用程序中添加自定义字体(iOS默认字体除外)。

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); 你可以像这样使用它, yourLabel.font = appFontRegular(12); or yourTextField.font = appFontBold(14); 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:] ; 你的代码看起来很干净,不需要写[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]

Add your custom font into your project , ie Dragged the font file(CALIBRIZ_0.TTF) into XCode project. 将自定义字体添加到项目中,即将字体文件(CALIBRIZ_0.TTF)拖动到XCode项目中。

Edit Info.plist : Add a new entry with the key " Fonts provided by application ". 编辑Info.plist :添加一个新条目,其中包含“ Fonts provided by application ”键。

For each of your files, add the file name to this array 对于每个文件,将文件名添加到此array

Now set font to your label 现在将字体设置为label

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

If you want to change all fonts for all UILabel in one place, you need to create a category of UILabel. 如果要在一个位置更改所有UILabel的所有字体,则需要创建一个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. 在创建类别后,您需要为所有文件导入此类,或者您可以将其添加到项目的“.pch”文件中。

same solution for buttons / textview .. 按钮/ textview的相同解决方案..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM