ios ios中的自定义字体不起作用

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

Custom Font in ios not working

iosiphonecocoa-touchuifontcustom-font

提问by the Reverend

I want to use HelveticaNeue-UltraLight in my ios application. I'm adding the font file as a resource to my project and adding the "Fonts provided by application" key in the plist file. The file name is HelveticaNeue.dfont and I've added it to the key array.

我想在我的 ios 应用程序中使用 HelveticaNeue-UltraLight。我正在将字体文件作为资源添加到我的项目中,并在 plist 文件中添加“应用程序提供的字体”键。文件名是 HelveticaNeue.dfont,我已将其添加到键数组中。

When I check for the available fonts I can see it now...

当我检查可用字体时,我现在可以看到它...

  NSArray *fonts = [UIFont fontNamesForFamilyName:@"Helvetica Neue"];

for(NSString *string in fonts){
    NSLog(@"%@", string);
}

1-07-08 17:32:57.866 myApp[5159:207] HelveticaNeue-Bold
2011-07-08 17:32:57.866 myApp[5159:207] HelveticaNeue-CondensedBlack
2011-07-08 17:32:57.867 myApp[5159:207] HelveticaNeue-Medium
2011-07-08 17:32:57.867 myApp[5159:207] HelveticaNeue
2011-07-08 17:32:57.868 myApp[5159:207] HelveticaNeue-Light
2011-07-08 17:32:57.868 myApp[5159:207] HelveticaNeue-CondensedBold
2011-07-08 17:32:57.868 myApp[5159:207] HelveticaNeue-LightItalic
2011-07-08 17:32:57.869 myApp[5159:207] HelveticaNeue-UltraLightItalic
2011-07-08 17:32:57.869 myApp[5159:207] HelveticaNeue-UltraLight // HERE IT IS!
2011-07-08 17:32:57.869 myApp[5159:207] HelveticaNeue-BoldItalic
2011-07-08 17:32:57.870 myApp[5159:207] HelveticaNeue-Italic

But when I try to get it with [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:12] I just get HelveticaNeue-Light..

但是当我尝试使用 [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:12] 获取它时,我只得到 HelveticaNeue-Light ..

I'm getting no errors or warnings. Help please!

我没有收到任何错误或警告。请帮忙!

回答by Ben G

Not only does the file needs to be added to the project, but it needs to be added to the target as well.

不仅需要将文件添加到项目中,还需要将其添加到目标中。

Sometimes (as in my case), when drag and dropping the ".ttf" file in Xcode, the "add to target" is not checked. This means the file is actually visible in your project, but it is not embedded in the app.

有时(如我的情况),在 Xcode 中拖放“.ttf”文件时,不会选中“添加到目标”。这意味着该文件实际上在您的项目中可见,但并未嵌入到应用程序中。

To make sure it is:

为了确保它是:

  • Click on the project's name (left pane)
  • Then on the target (middle pane)
  • Then "Build Phases" (third tab on the right pane)
  • Then "Copy Bundle Resources"
  • 单击项目名称(左窗格)
  • 然后在目标上(中间窗格)
  • 然后“构建阶段”(右侧窗格中的第三个选项卡)
  • 然后“复制捆绑资源”

You should see the font file in that list.

您应该会在该列表中看到字体文件。

回答by Thibault Martin-Lagardette

I think you're having the same problem I had : there's a bug in iOS where it is not able to use more than 2 fonts from the same family.

我认为您遇到了与我相同的问题:iOS 中存在一个错误,它无法使用来自同一系列的 2 种以上字体。

See blog post here for details and solution : http://www.pixeldock.com/blog/uifont-problem-when-using-more-than-2-custom-fonts-of-the-same-font-family/

有关详细信息和解决方案,请参阅此处的博客文章:http: //www.pixeldock.com/blog/uifont-problem-when-using-more-than-2-custom-fonts-of-the-same-font-family/

回答by Baljeet Singh

after adding Font file in Bundle "Font Provided by Application"property click on drop down arrow and enter your font name with extension

在捆绑“应用程序提供的字体”属性中添加字体文件后, 单击下拉箭头并输入带有扩展名的字体名称

回答by TomCobo

It is working with different variations of the same font(even in IOS 8). I'll post my mistake just in case someone has the same problem... I was using font's filename instead of the font name. A useful solution is print all the fonts available and look for the one I was using. In Swift would be:

它使用相同字体的不同变体(即使在 IOS 8 中)。我会发布我的错误以防万一有人遇到同样的问题......我使用的是字体的文件名而不是字体名称。一个有用的解决方案是打印所有可用的字体并查找我正在使用的字体。在 Swift 中将是:

for fontFamilyNames in UIFont.familyNames {
    for fontName in UIFont.fontNames(forFamilyName: fontFamilyNames) {
        print("FONTNAME:\(fontName)")
    }
}

I found this useful tutorial in: Code with Chris

我在以下位置找到了这个有用的教程:Code with Chris

回答by andresousa

Open Font Book application. If you installed the fonts yourself, go to user, look for the fonts you want and use the PostScript name of the font in your xcode project.

打开字体书应用程序。如果您自己安装了字体,请转到用户,查找您想要的字体并在 xcode 项目中使用字体的 PostScript 名称。

It should work even for different font variations of the same family.

它甚至应该适用于同一系列的不同字体变体。

回答by sho

While this probably won't help the OP, it might be useful to others.

虽然这可能不会帮助 OP,但它可能对其他人有用。

With iOS 5, Helvetica Neueis automatically included; you don't have to add this custom font to the bundle. It's even possible to use that font family directly from Interface Builder, which is extremely handy.

使用 iOS 5,Helvetica Neue自动包含;您不必将此自定义字体添加到捆绑包中。甚至可以直接从 Interface Builder 使用该字体系列,这非常方便。

回答by vishiphone

Try this this will be also heleful for you.

试试这个,这对你也很有帮助。

    [compete_Label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];

回答by Jano

Try with spaces:

尝试使用空格:

[UIFont fontWithName:@"Helvetica Neue UltraLight" size:12];

See Certain fonts not showing up?

请参阅某些字体未显示?

回答by the Reverend

I ended up buying the font Helvetica Neue UltraLight from a third party, added it with no problems.

我最终从第三方购买了字体 Helvetica Neue UltraLight,添加它没有任何问题。

回答by Sayka

After spending hours, I came to know that the Name for the font we need to add is not the file name of the font but the actual Font name. I was trying to include font file bonveno.ttf. I have included the filename along with extension in info.plist and was able to see the font in the Copy Bundle Resourceslist. My code was like

花了几个小时后,我才知道我们需要添加的字体的名称不是字体的文件名,而是实际的字体名称。我试图包含字体文件bonveno.ttf。我在 info.plist 中包含了文件名和扩展名,并且能够在Copy Bundle Resources列表中看到字体。我的代码就像

self.label.font = [UIFont fontWithName:@"bonveno" size:30];

that was causing the problem. Then I double clicked the font file from Finderto see the preview of the font. That time I noticed the name of the font in the preview window as BonvenoCF. So I added that name in the code like

那是造成问题的原因。然后我从Finder 中双击字体文件以查看字体的预览。那一次我注意到预览窗口中字体的名称是BonvenoCF。所以我在代码中添加了这个名字

self.label.font = [UIFont fontWithName:@"BonvenoCF" size:30];

Worked like charm!! Can't live without this cute font in my apps.

像魅力一样工作!!在我的应用程序中没有这种可爱的字体就活不下去。