Xcode 8自定义字体未显示在界面生成器中 [英] Xcode 8 custom font doesn't show up in interface builder

查看:95
本文介绍了Xcode 8自定义字体未显示在界面生成器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. 将.otf文件复制到项目中.
  2. 确认.otf文件已将项目设置为目标.
  3. 在plist的应用程序提供的字体"中添加了.otf文件.
  4. 在构建阶段,.otf文件位于副本捆绑资源"中.
  5. 在Mac上安装字体.
  6. 尝试打印所有可用字体,但看不到我的自定义字体.

我使用的代码:

for name in UIFont.familyNames() {
  println(name)
  if let nameString = name as? String
  {
    println(UIFont.fontNamesForFamilyName(nameString))
  }
}

  1. 尝试在代码中设置字体,并且有效.

我使用的代码:

textLabel?.font = UIFont(name: "BebasNeueRegular", size: 14)

  1. 但是我不能在界面生成器中设置它.有什么想法吗?

屏幕截图:

推荐答案

尝试以下步骤:Swift 3中测试过的代码.

第1步:将自定义字体添加到项目中(请确保添加到目标 我正在使用"PermanentMarker.ttf","Pecita.otf"和"AROLY.ttf"字体作为测试字体.

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

注意:支持的字体类型 ttf otf (两种字体类型均应工作)

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

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

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".

您的 plist 应该看起来像这样

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

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

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

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

更新2: Working Solution

之后,导入自定义字体并更新plist.从storyBoard中选择label,在> >下转到Attributes Inspector,选择Attributed,然后选择custom font从列表中.

After, imported your custom font and updated your plist.selectlabel from your storyBoard,goto Attributes Inspectorunder Label>Text type> select to Attributed and choose your custom font from the list.

输出:

更新1

如果您的自定义字体仍未在Xcode font list中列出,请检查与您的问题相关的链接

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

自定义字体未显示在某些模拟器上

注意:不过,您可以通过编程方式将BebasNeuecustom font分配给标签或按钮等,即使其未显示在interface Builder中.如果在设置字体时遇到麻烦以编程方式访问对象.请尝试以下方法.

Note: Still,You can assign BebasNeue or custom font programatically 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.

将字体分配给UILabel:

Assign font to UILabel:

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

将字体分配给UIButton:

Assign font to UIButton:

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

将字体分配给UITextField:

Assign font to UITextField:

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

将字体分配给UINavigationBar:

Assign font to UINavigationBar:

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

将字体分配给UIToolBar:

Assign font to UIToolBar:

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

输出:

这篇关于Xcode 8自定义字体未显示在界面生成器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆