Xcode 5 中的自定义字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18941850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:55:59  来源:igfitidea点击:

Custom Fonts in Xcode 5

iphonexcodefontsxcode5

提问by Mr Big Problem

How would you add a custom font in Xcode 5 and how would you change every label in the project to that font? Because I've heard you can only do this programmatically?

您将如何在 Xcode 5 中添加自定义字体以及如何将项目中的每个标签更改为该字体?因为我听说您只能以编程方式执行此操作?

采纳答案by Daniel Swan

Much better option is to add the new font to your system/OS fonts directory and then from there it can be added in the storyboard or used with NSAttributed strings.

更好的选择是将新字体添加到您的系统/操作系统字体目录,然后从那里它可以添加到故事板中或与 NSAttributed 字符串一起使用。

Steps:

脚步:

  1. Download your font .ttf files
  2. Go to any label/textview in your storyboard and select in top right attributes inspector: "Text" Then select > "Attributed"
  3. Click the little T icon next to the font name to open up the fonts editor
  4. Go to the cog/wheel/setting icon and select "Manage Fonts"
  5. Click the second from right "+" button and navigate to your fonts .ttf file to add to system fonts collection
  6. Now use your font by changing the font name in the storyboard or code :)
  1. 下载您的字体 .ttf 文件
  2. 转到故事板中的任何标签/文本视图并在右上角的属性检查器中选择:“文本”然后选择>“属性”
  3. 单击字体名称旁边的小 T 图标打开字体编辑器
  4. 转到齿轮/滚轮/设置图标并选择“管理字体”
  5. 单击右数第二个“+”按钮并导航到您的字体 .ttf 文件以添加到系统字体集合
  6. 现在通过更改故事板或代码中的字体名称来使用您的字体:)

Hope that helps!

希望有帮助!

回答by Jordan Montel

You need to set every label programmatically with your custom Font.

您需要使用自定义字体以编程方式设置每个标签。

To use custom font :

使用自定义字体:

1/ add your custom font in your project like resources (font .ttf or .otf)

1/ 在您的项目中添加您的自定义字体,如资源(字体 .ttf 或 .otf)

2/ in your info.plist add key UIAppFonts (Fonts provided by application) and and the name of each custom font (for example : SohoGothicStd.ttf)

2/ 在您的 info.plist 中添加关键 UIAppFonts(应用程序提供的字体)和每个自定义字体的名称(例如:SohoGothicStd.ttf)

3/ you can create macro for use your font

3/您可以创建宏以使用您的字体

#define FONT_SOHO_STD(s) [UIFont fontWithName:@"SohoGothicStd" size:s]

4/ use this macro for a label par exemple :

4/ 将此宏用于标签标准示例:

_myLabel.font = FONT_SOHO_STD(15.0f);

回答by Yas T.

I believe you need to use [UILabel appearance]proxy to set custom font for all labels across your application. Add following lines to your AppDelegatedidFinishLaunchingWithOptionsfunction to set custom font for all UILabels in your project.

我相信您需要使用[UILabel appearance]代理为应用程序中的所有标签设置自定义字体。将以AppDelegatedidFinishLaunchingWithOptions下行添加到您的函数中,以便为UILabel项目中的所有s设置自定义字体。

UIFont *newFont = [UIFont fontWithName:@"My-Custom-Font-Name" size:14];
[[UILabel appearance] setFont:newFont];

NOTE: You need to make sure your fonts are in your Xcode project.

注意:您需要确保您的字体在您的 Xcode 项目中。

Here are the steps you can follow to add custom font to the project.

您可以按照以下步骤将自定义字体添加到项目中。

  1. Add your custom font files into your project using Xcodeas a resource
  2. Add a key to your Info.plistfile called UIAppFonts.
  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 UIAppFontsarray
  5. Save Info.plist
  1. 将您的自定义字体文件Xcode用作资源添加到您的项目中
  2. 向您的Info.plist文件中添加一个名为UIAppFonts.
  3. 将此键设为数组
  4. 对于您拥有的每种字体,输入字体文件的全名(包括扩展名)作为UIAppFonts数组的项
  5. 节省 Info.plist

Steps taken from: Can I embed a custom font in an iPhone application?

步骤取自:我可以在 iPhone 应用程序中嵌入自定义字体吗?

回答by Bishal Ghimire

Yes you need to do it through code. I dont think XCode5 has added any new feature like such. To do it programmatically do the following :

是的,您需要通过代码来完成。我认为 XCode5 没有添加任何这样的新功能。要以编程方式执行以下操作:

Make a class which is subclass of UILabel then say you have something like Title which has font size 14 with Helverica-Bold

创建一个类,它是 UILabel 的子类,然后说你有类似 Title 的东西,它的字体大小为 14,带有 Helverica-Bold

IN .h file

在 .h 文件中

#import <UIKit/UIKit.h>

@interface MyLabel : UILabel {

}

@end

In .m file

在 .m 文件中

    #import "MyLabel.h"

    @implementation MyLabel

    - (id)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        self.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    }
    return self;
}

    @end

Another example without using separate sub class

另一个不使用单独子类的例子

-(UILabel *) myTitleLabel {
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor=[UIColor clearColor];
    [label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
    label.textColor=[UIColor colorWithRed:(79.0 / 255.0) green:(79.0 / 255.0) blue:(79.0 / 255.0) alpha: 1];
    [label setTextAlignment:NSTextAlignmentLeft];
    [label sizeToFit];
    return label;
}

Then you can use myTitleLabel class to create a object so that all the titleLabel Object will have same color and font size.

然后你可以使用 myTitleLabel 类来创建一个对象,这样所有的 titleLabel 对象都将具有相同的颜色和字体大小。

回答by Alper

The answers here are fine, but I'd recommend setting as little as possible in your custom label and definitely not hardcoding the size. You can easily do it like this:

这里的答案很好,但我建议在您的自定义标签中设置尽可能少,绝对不要对大小进行硬编码。你可以很容易地这样做:

#import "CustomFontLabel.h"

@implementation CustomFontLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setFontStyle];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];

    if (self) {
        [self setFontStyle];
    }

    return self;
}

- (void)setFontStyle {
    CGFloat fontSize = self.font.pointSize;

    [self setFont:[UIFont fontWithName:@"LindenHill" size:fontSize]];
}

@end

This will respect all of the other settings you set in your Storyboard making it way easier to manage everything.

这将尊重您在 Storyboard 中设置的所有其他设置,从而更轻松地管理所有内容。

回答by Jayprakash Dubey

You can create custom font in Xcode using following steps :

您可以使用以下步骤在 Xcode 中创建自定义字体:

  1. Import font type into your project (*.ttf file)
  2. Add “Fonts provided by application”key into plist of your project.
  3. Add custom font as an item array in “Fonts provided by application” key.
  4. Now, you can use font in label as :

    [lblName setFont:[UIFont fontWithName:@“CustomFontName” size:14.0]];

  1. 将字体类型导入您的项目(*.ttf 文件)
  2. “应用程序提供的字体”键添加到项目的 plist 中。
  3. 在“应用程序提供的字体”键中添加自定义字体作为项目数组。
  4. 现在,您可以将标签中的字体用作:

    [lblName setFont:[UIFont fontWithName:@“CustomFontName” size:14.0]];