ios Xcode 8 自定义字体未显示在界面构建器中

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

Xcode 8 custom font doesn't show up in interface builder

iosswiftxcodecustom-font

提问by chengsam

I am trying to use custom font (Bebas Neue) in my iOS application. The steps I took are:

我正在尝试在我的 iOS 应用程序中使用自定义字体 (Bebas Neue)。我采取的步骤是:

  1. Copy the .otf files to the project.
  2. Confirm the .otf files have set the project as target.
  3. Added the .otf files in 'Fonts provided by application' in plist.
  4. In Build Phases, the .otf files are in 'Copy Bundle Resources'.
  5. Install the font on my Mac.
  6. Try to print out all fonts available but I can't see my custom font.
  1. 将 .otf 文件复制到项目中。
  2. 确认 .otf 文件已将项目设置为目标。
  3. 在 plist 的“应用程序提供的字体”中添加了 .otf 文件。
  4. 在构建阶段,.otf 文件位于“复制捆绑资源”中。
  5. 在我的 Mac 上安装字体。
  6. 尝试打印出所有可用的字体,但我看不到我的自定义字体。

The code I used:

我使用的代码:

for name in UIFont.familyNames() {
  println(name)
  if let nameString = name as? String
  {
    println(UIFont.fontNamesForFamilyName(nameString))
  }
}
  1. Trying to set the font in code and it worked.
  1. 尝试在代码中设置字体并且它起作用了。

The code I used:

我使用的代码:

textLabel?.font = UIFont(name: "BebasNeueRegular", size: 14)
  1. But I can't set it in interface builder. Any idea?
  1. 但是我不能在界面生成器中设置它。任何的想法?

Screenshots:

截图:

FontFont-List

字体字体列表

回答by Joe

Try Below Steps:Code tested in Swift 3.

尝试以下步骤:Swift 3.

Step 1: Add Your custom font into your project( Make sure Add to Targetticked).I am using "PermanentMarker.ttf","Pecita.otf" and "AROLY.ttf" font as a test font.

第 1 步:将您的自定义字体添加到您的项目中(确保 勾选添加到目标)。我使用“PermanentMarker.ttf”、“Pecita.otf”和“AROLY.ttf”字体作为测试字体。

Note:Supporting font Type ttfand otf(Both font types should work)

注意:支持字体类型ttfotf(两种字体类型都应该工作)

Step 2: Modify the application-info.plist file. Add the key "Fonts provided by application" in a new row and add "PermanentMarker.ttf" as new item in the Array "Fonts provided by application".

第二步:修改application-info.plist文件。在新行中添加键“应用程序提供的字体”,并在“应用程序提供的字体”数组中添加“PermanentMarker.ttf”作为新项目。

Your plistshould looks like this

你的plist应该是这样的

enter image description here

在此处输入图片说明

Now the font will be available in Interface Builder. To use the custom font in code we need to refer to it by name, but the name often isn't the same as the font's filename

现在该字体将在 Interface Builder 中可用。要在代码中使用自定义字体,我们需要通过名称来引用它,但名称通常与字体的文件名不同

Now, You can access the Custom Font from your viewController. I am testing the font by placing a UIlabel to the Storyboard like below.

现在,您可以从 viewController 访问自定义字体。我正在通过将 UIlabel 放置到 Storyboard 来测试字体,如下所示。

Update 2:Working Solution

更新 2:Working Solution

After, imported your custom font and updated your plist.selectlabelfrom your storyBoard,goto Attributes Inspectorunder Label>Text type> select to Attributedand choose your custom fontfrom the list.

之后,进口自定义字体和更新了plist。选择label从您storyBoard,转到Attributes InspectorLabel> Text type>选择Attributed,然后选择您custom font从列表中。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Output:

输出:

enter image description here

在此处输入图片说明

Update 1

更新 1

If your custom font still not listed in Xcode font list.check the related link to your issue

如果您的自定义字体仍未在Xcode font list.check 相关链接中列出您的问题

Note:Still,You can assign BebasNeueor custom fontprogramatically to your label or button etc. even its not showing in your interface Builder.If you having trouble setting font to your object programatically.try below method.

注意:仍然,您可以分配BebasNeue或以custom font编程方式分配给您的标签或按钮等。即使它没有显示在您的interface Builder.

Assign font to UILabel:

为 UILabel 分配字体:

    label?.font = UIFont(name: "BebasNeue", size: 35) // Set to any size

Assign font to UIButton:

为 UIButton 分配字体:

    button.titleLabel?.font = UIFont(name: "BebasNeue", size: 35)

Assign font to UITextField:

为 UITextField 分配字体:

    textField.font = UIFont(name: "BebasNeue", size: 25) 

Assign font to UINavigationBar:

为 UINavigationBar 分配字体:

    navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "BebasNeue", size: 25)!, NSForegroundColorAttributeName: UIColor.red]

Assign font to UIToolBar:

为 UIToolBar 分配字体:

    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "BebasNeue", size: 25.0)!], for: UIControlState.normal)

Output:

输出:

enter image description here

在此处输入图片说明

回答by Saranjith

Its Easy and simple now- Tested in Xcode 10 and swift 5

现在简单易用-在 Xcode 10 和 swift 5 中测试

Steps to add font to your Xcode

向 Xcode 添加字体的步骤

Select your UILabel, UITextField or whatever then under fonts section and follow

选择您的 UILabel、UITextField 或其他字体部分,然后按照

Step 1

第1步

Select settings menu from left corner of font selection screen. And choose font manager option.

从字体选择屏幕的左上角选择设置菜单。并选择字体管理器选项。

enter image description here

在此处输入图片说明

Step 2

第2步

Click on the add button as marked below.

单击下面标记的添加按钮。

enter image description here

在此处输入图片说明

Step 3

第 3 步

Select the folder that contains your font. It will be loaded into XCode fonts list.

选择包含字体的文件夹。它将被加载到 XCode 字体列表中。

Steps to add fonts to your project

将字体添加到项目的步骤

Don't forget to add description to your plist with the key Fonts provided by applicationand put font files inside copy bundle resources under project target settings.

不要忘记使用密钥向 plist 添加描述Fonts provided by application,并将字体文件放在项目目标设置下的复制包资源中。

Yes! thats it.. njoy..

是的!就是这样..njoy..

回答by Flitskikker

When using the same font (Bebas Neue), I experience the exact same problem: the font does not show up in the font list for Plaincontrols.

使用相同的字体 (Bebas Neue) 时,我遇到了完全相同的问题:该字体未显示在Plain控件的字体列表中。

It does for Attributedcontrols, as described in Update 2 in Joes post, but then you're actually changing the attributedTextinstead of the regular text, which may lead to unwanted behavior. For example, you'll have to fiddle with NSMutableAttributedStringto change the text or text color at runtime.

它适用于Attributed控件,如 Joes 帖子中的更新 2 中所述,但实际上您正在更改attributedText而不是常规text,这可能会导致不需要的行为。例如,您必须NSMutableAttributedString在运行时更改文本或文本颜色。

I did some investigation on this issue. I used FontLab Studioto modify the font and do some tests. I used Bebas Neue version 1.400 (dated September 15, 2014) from dafont.com.

我对这个问题做了一些调查。我使用FontLab Studio修改字体并进行了一些测试。我使用了来自 dafont.com 的 Bebas Neue 1.400 版(日期为 2014 年 9 月 15 日)

  • Maybe the font file was somehow corrupted. Re-saved as .otf and .ttf: Didn't work.
  • Maybe the meta data of the font were corrupted. Copied all the glyphs to a new font and named that Bebas Neue: Didn't work.
  • I renamed the font from Bebas Neue to BN: It works!But why?
  • Maybe you can't use "Neue" in a font name (since Helvetica Neue was the iOS system font up to iOS 8). Renamed the font to Test Neue: Still works.
  • Maybe you can't use "Bebas" in a font name then? Renamed the font to Bebas: Still works.
  • What... Just to be sure, I changed the font name back to Bebas Neue: Doesn't work again.
  • I also tried BebasNeue: Also didn't work.
  • Then I changed the name to Bebas Neue Whatever: It works.

    Bebas Neue Whatever

  • 也许字体文件以某种方式损坏了。重新保存为 .otf 和 .ttf:无效。
  • 也许字体的元数据已损坏。将所有字形复制到新字体并命名为 Bebas Neue:不起作用。
  • 我将字体从 Bebas Neue 重命名为 BN:它有效!但为什么?
  • 也许您不能在字体名称中使用“Neue”(因为 Helvetica Neue 是 iOS 8 之前的 iOS 系统字体)。将字体重命名为 Test Neue:仍然有效。
  • 也许你不能在字体名称中使用“Bebas”?将字体重命名为 Bebas:仍然有效。
  • 什么... 可以肯定的是,我将字体名称改回 Bebas Neue:不再起作用。
  • 我也试过 BebasNeue:也没有用。
  • 然后我将名称更改为 Bebas Neue 无论如何:它有效。

    Bebas Neue 随便

I really do not understand why this is happening. Doesn't Apple or Xcode want you to use "Bebas Neue" or "BebasNeue" in the Interface Builder? I had the normal Bebas Neue installed. Maybe the reference got corrupted? I really can't tell. Maybe someone on a clean system can try if Bebas Neue works in their Xcode.

我真的不明白为什么会这样。Apple 或 Xcode 不希望您在 Interface Builder 中使用“Bebas Neue”或“BebasNeue”吗?我安装了普通的 Bebas Neue。也许引用已损坏?我真的说不出来。如果 Bebas Neue 在他们的 Xcode 中工作,也许干净系统上的人可以尝试。

Anyway, I achieved my goal: being able to use Bebas Neue (I named the final version "Bebas Neue MyAppName") for Plainstyled labels and buttons, so I can design my app as-is and don't have to fiddle around with attributed strings.

无论如何,我实现了我的目标:能够使用 Bebas Neue(我将最终版本命名为“Bebas Neue MyAppName”)作为Plain样式标签和按钮,因此我可以按原样设计我的应用程序,而不必摆弄属性字符串。

回答by János

Finally, I found the root causeof issues with Bebas Neue fonts.

最后,我找到了Bebas Neue 字体问题的根本原因

I don't remember where I have downloaded the fonts, but Bebas Neue fontsare inconsistent.

我不记得在那里我已经下载的字体,但自由的字体抵达Neue不一致的

Downloadi.e. app called Typelight

下载ie 应用程序调用Typelight

Open not Regular, but i.e. Boldfont with Typelight.

打开不是常规,而是粗体字体Typelight

enter image description here

在此处输入图片说明

You need correct most of the Bebas Neuefonts.

您需要更正大部分Bebas Neue字体。

回答by Andrew Van Beek

You can actually just go into attributed fonts, click the custom font window. There click the gear in the upper left and click on the option manage fonts. A window should pop up and on the top and there should be a plus sign to allow you to add fonts. Just choose the custom font you downloaded like xyz.tff and then it should be added.

您实际上可以进入属性字体,单击自定义字体窗口。单击左上角的齿轮,然后单击管理字体选项。应该弹出一个窗口并在顶部,应该有一个加号以允许您添加字体。只需选择您下载的自定义字体(如 xyz.tff),然后将其添加。

Important to note is that ttf file should be referenced in the p-list as well as in your project directory!

重要的是要注意 ttf 文件应该在 p-list 以及您的项目目录中引用!

回答by Mehmet Salih Aslan

maybe there is a difference between folder name and font name the solution would be:

也许文件夹名称和字体名称之间存在差异,解决方案是:

  • Print all default fonts names.(without custom fonts)
  • Add custom fonts to project.
  • Print all fonts names.
  • 打印所有默认字体名称。(不含自定义字体)
  • 将自定义字体添加到项目。
  • 打印所有字体名称。

and after that compare fonts names.

然后比较字体名称。

func printFontsNames() {
    let fontFamilyNames = UIFont.familyNames
    for familyName in fontFamilyNames {
        print("------------------------------")
        print("Font Family Name = [\(familyName)]")
        let names = UIFont.fontNames(forFamilyName: familyName )
        print("Font Names = [\(names)]")
    }
}

回答by wj2061

It was an old bugsince Xcode 3.x:

这是自 Xcode 3.x 以来的旧错误

there is a bug/known issue with IB on XCode 3.x with non-standard fonts and Interface Builder. Custom fonts generally need to be set programatically (via [UIFont fontWithName:])

带有非标准字体和界面生成器的 XCode 3.x 上的 IB 存在错误/已知问题。自定义字体通常需要以编程方式设置(通过 [UIFont fontWithName:])

There are some work around like thiswith User Defined Runtime Attributeand categories.

有一些像这样的解决方法User Defined Runtime Attributecategories

回答by Vishal16

I had similar problem

我有类似的问题

added custom font and also assign it to UILabelall working fine but after a few times later it's wont work as expected.

添加了自定义字体并将其分配给UILabel所有工作正常但几次后它不会按预期工作。

Trying all solution but nothing work for me. So here is my solution, again going to previous UILabel(already have newly added custom font) select->label->select newly added font. Check other UI controls Font.

尝试所有解决方案,但对我没有任何作用。所以这是我的解决方案,再次转到上一个UILabel(已经有新添加的自定义字体)选择->标签->选择新添加的字体。检查其他 UI 控件字体。

now it's working fine.

现在它工作正常。

Hope this help newDevs...

希望这有助于新开发者...

回答by alstr

This is still a recurring issue for me with Swift 4.2, even without making any changes that would cause it.

对于 Swift 4.2,这对我来说仍然是一个反复出现的问题,即使没有进行任何会导致它的更改。

Removing the font files from the Xcode project and adding them again fixes the problem (until next time).

从 Xcode 项目中删除字体文件并再次添加它们可以解决问题(直到下一次)。

回答by ViktoR

In my case, a font family was missing in Interface builder, because one of the fonts from that family was not added to the project, but was indicated in the info.plist file.

就我而言,Interface builder 中缺少一个字体系列,因为该系列中的一种字体未添加到项目中,但已在 info.plist 文件中指明。

Generally removing all my custom fonts from the project and adding them again - fixed the issue.

通常从项目中删除我所有的自定义字体并再次添加它们 - 解决了这个问题。