Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

Font Font-List

like image 741
chengsam Avatar asked Oct 21 '16 04:10

chengsam


People also ask

How do I add custom fonts to Xcode?

To add a font file to your Xcode project, select File > Add Files to “Your Project Name” from the menu bar, or drag the file from Finder and drop it into your Xcode project. You can add True Type Font (. ttf) and Open Type Font (. otf) files.

How do I use custom fonts in Swift UI?

To use a custom font, add the font file that contains your licensed font to your app, and then apply the font to a text view or set it as a default font within a container view. SwiftUI's adaptive text display scales the font automtically using Dynamic Type.

What is system font in Xcode?

SanFranciscoUIDisplay is used for titles and larger font wheres SanFranciscoText is used for paragraph text and smaller font. They are the System Font in Xcode or you can download them here (but you need a developer account): developer.apple.com/fonts/


2 Answers

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

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

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.

enter image description here

enter image description here

enter image description here

Output:

enter image description here

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:

enter image description here

like image 144
Joe Avatar answered Oct 21 '22 09:10

Joe


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.

enter image description here

Step 2

Click on the add button as marked below.

enter image description here

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

like image 27
Saranjith Avatar answered Oct 21 '22 09:10

Saranjith