添加了在 Xcode 中不起作用的自定义字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14109047/
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-15 02:26:11  来源:igfitidea点击:

Added custom font not working in Xcode

iphonexcode

提问by DeepK SOreadytohelp

I am trying to add a specific font in my app. I did all the work like adding the font to resources, to plist and to Copy Bundle Resources. I even added font classes as well and also googled a lot. But all in vain, nothing worked for me.

我正在尝试在我的应用程序中添加特定字体。我做了所有的工作,比如将字体添加到资源、plist 和复制捆绑资源。我什至还添加了字体类,并且还搜索了很多。但一切都是徒劳的,没有什么对我有用。

回答by Anoop Vaidya

Custom Font Support Applications that want to use custom fonts can now include those fonts in their application bundle and register those fonts with the system by including the UIAppFonts key in their Info.plist file. The value of this key is an array of strings identifying the font files in the application's bundle. When the system sees the key, it loads the specified fonts and makes them available to the application.

自定义字体支持想要使用自定义字体的应用程序现在可以将这些字体包含在其应用程序包中,并通过在其 Info.plist 文件中包含 UIAppFonts 键来向系统注册这些字体。此键的值是一个字符串数组,用于标识应用程序包中的字体文件。当系统看到密钥时,它会加载指定的字体并使它们可供应用程序使用。

Once the fonts have been set in the Info.plist, you can use your custom fonts as any other font in IB or programatically.

一旦在 Info.plist 中设置了字体,您就可以像在 IB 中或以编程方式使用任何其他字体一样使用您的自定义字体。

There is an ongoing thread on Apple Developer Forums: https://devforums.apple.com/thread/37824(login required)

Apple 开发者论坛上有一个正在进行的线程:https: //devforums.apple.com/thread/37824(需要登录)

And here's an excellent tutorialon how to achieve this.

这是一个关于如何实现这一目标的优秀教程

Here are the steps transcribed:

以下是转录步骤:

Add your custom font files into your project using XCode as a resource

使用 XCode 作为资源将您的自定义字体文件添加到您的项目中

Add a key to your info.plist file called UIAppFonts.

在 info.plist 文件中添加一个名为 UIAppFonts 的键。

Make this key an array

将此键设为数组

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

对于您拥有的每种字体,输入字体文件的全名(包括扩展名)作为 UIAppFonts 数组的项

Save info.plist

保存 info.plist

Now in your application you can simply call [UIFont fontWithName:@"CustomFontName" size:12] to get the custom font to use with your UILabels and UITextViews, etc…

现在在您的应用程序中,您可以简单地调用 [UIFont fontWithName:@"CustomFontName" size:12] 来获取自定义字体以与您的 UILabels 和 UITextViews 等一起使用......

Also: Make sure the fonts are in your Copy Bundle Resources.

另外:确保字体在您的复制包资源中。

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

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

回答by Luda

Happy New Year to you too. I had this problem. My problem was that the name of the font in the p.list was written with capital letters. When I wrote the name in the .plist the same as in the file name, it was fixed.

也祝你新年快乐。我有这个问题。我的问题是 p.list 中字体的名称是用大写字母写的。当我在 .plist 中写入与文件名相同的名称时,它被修复了。

回答by junaidsidhu

Check the Font Label, That will help you, Screen Shot of Font Labelis attached

检查字体标签,这对你有帮助,附上字体标签的屏幕截图

enter image description here

在此处输入图片说明

回答by Exploring

Check the name of the font you have added into the plist are same as the name of the font files. Then write the following code nslog (@"%@", [uifont familynames]); Check the name of the font you have added from the list in nslog and apply that hope it will work for you. Check my spelling mistakes and cases for the class names or method names.

检查您添加到 plist 中的字体名称是否与字体文件的名称相同。然后写如下代码nslog(@"%@", [uifont familynames]); 检查您从 nslog 列表中添加的字体名称并应用它,希望它对您有用。检查我的拼写错误和类名或方法名的大小写。