简体   繁体   English

ttf字体在界面构建器或模拟器中不适用于iPhone

[英]ttf font not available for iPhone in interface builder or simulator

I added a font file (.ttf) to my Xcode project, to resources. 我在我的Xcode项目中添加了一个字体文件(.ttf)到资源。 Also, I added it to the UIAppFonts in my info.plist. 另外,我将它添加到我的info.plist中的UIAppFonts。

When I want to use this font though, I don't even see it as a choice in IB. 当我想使用这种字体时,我甚至不认为它是IB的选择。

After installing the font on my system, I started seeing it in IB, but still - changing to it doesn't change anything - some default system font is displayed in the Interface Builder as well as in iPhone emulator. 在我的系统上安装字体之后,我开始在IB中看到它,但仍然 - 改变它并没有改变任何东西 - 一些默认的系统字体显示在Interface Builder和iPhone模拟器中。

Are there any steps more I should do to be able to use my own font? 为了能够使用我自己的字体,我还有更多的步骤吗?

To use custom fonts with iOS you have to set them programmatically. 要在iOS上使用自定义字体,您必须以编程方式设置它们。

For example, suppose you have a font file called swellfont.ttf that you add to your project. 例如,假设您有一个名为swellfont.ttf的字体文件,您将其添加到项目中。 You then would go into your App-Info.plist file and add the full name of the file to the next index of the array keyed by UIAppFonts, as you mention. 然后,您将进入App-Info.plist文件,并将文件的全名添加到UIAppFonts键入的数组的下一个索引中,如您所述。

<key>UIAppFonts</key>
<array>
    <string>swellfont.ttf</string>
</array>

Then, to use the font: 然后,使用字体:

label.font = [UIFont fontWithName:@"swellfont" size:12];

Assuming that label is a UILabel and that swellfont.ttf is not protected . 假设标签是UILabel并且swellfont.ttf不受保护 It's important to note that UIFont's fontWithName is not referring to the filename, instead its wanting the actual name of the font. 重要的是要注意UIFont的fontWithName不是指文件名,而是需要字体的实际名称。 If you open the font with FontForge you can see this information by selecting Element > Font Info from the menu bar. 如果使用FontForge打开字体,可以通过从菜单栏中选择元素>字体信息来查看此信息。 There are probably cleverer ways to find this information out. 可能有更聪明的方法可以找到这些信息。

I use 2 fonts in an APP. 我在APP中使用了2种字体。 The first one was automatically added to the "Copy Bundle Resources", the other was not. 第一个被自动添加到“Copy Bundle Resources”,另一个则没有。 I had to add that one myself. 我必须自己添加一个。

Call in code: 致电代码:

[labelName setFont:[UIFont fontWithName:@"OneGreatBigFont" size:40]];

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

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