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

我的未来我决定 提交于 2019-11-26 18:11:13

问题


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

  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.

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.

The code I used:

textLabel?.font = UIFont(name: "BebasNeueRegular", size: 14)
  1. But I can't set it in interface builder. Any idea?

Screenshots:


回答1:


Try Below Steps: Code tested in Swift 3.

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.

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

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

Your plist should looks like this

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

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

Update 2: Working Solution

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.

Output:

Update 1

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

  • http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

  • custom font not displaying on some simulator

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.

Assign font to UILabel:

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

Assign font to UIButton:

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

Assign font to UITextField:

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

Assign font to UINavigationBar:

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

Assign font to UIToolBar:

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

Output:




回答2:


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

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

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

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

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.

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




回答3:


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

Steps to add font to your Xcode

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

Step 1

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

Step 2

Click on the add button as marked below.

Step 3

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

Steps to add fonts to your project

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

Yes! thats it.. njoy..




回答4:


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

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

Download i.e. app called Typelight

Open not Regular, but i.e. Bold font with Typelight.

You need correct most of the Bebas Neue fonts.




回答5:


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.

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




回答6:


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)]")
    }
}



回答7:


It was an old bug since 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:])

There are some work around like this with User Defined Runtime Attribute and categories .




回答8:


I had similar problem

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

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.

now it's working fine.

Hope this help newDevs...




回答9:


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

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



来源:https://stackoverflow.com/questions/40168344/xcode-8-custom-font-doesnt-show-up-in-interface-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!