Using Custom Fonts in Interface Builder

Custom fonts in Interface Builder

Yes, Marrius is right. In Xcode 6, you neither need to use extra software nor to set Font in Code files. Just add your font files in your project along with other files. And Interface builder will display the added fonts in the Custom Font List.

And the most amazing thing is, xcode 6 shows the applied font instantly in IB. This is the great addition by Apple.

Also make sure to add this key "Fonts provided by application" in your project's info.plist and provide your font file names to see the effect in devices.

Using custom fonts in interface builder

I have also this problem in Xcode 4. In my program, there are lots of UILabel which have no IBOutlets so I do in that way;

First, subclass the UILabel to CustomFontLabel

Then, override the "awakeFromNib" method

@implementation CustomFontLabel

- (void)awakeFromNib {
[super awakeFromNib];
self.font = [UIFont fontWithName:@"CustomFontName" size:self.font.pointSize];
}

@end

Finally, in Interface Builder > Identity Inspector change class to CustomFontLabel.

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

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

Sample Image

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.

Sample Image

Sample Image

Sample Image

Output:

Sample Image

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:

Sample Image

Custom font with dynamic type in interface builder

No, you'd need to use the programmatic approach. However, it is easy to write a little loop that runs through all your labels and for each one, fetches the dynamic type "role" already set in interface builder and substitutes a UIFontMetrics value based on that "role" and the desired custom font.

Custom Fonts only available when set in Interface Builder

When I try to use it programmatically it doesn't work, and it doesn't show up when I print out a list of available fonts

This proves that you have not in fact included it properly in the target and the Info.plist.

The reason it seems to work in IB is that this font is also present on your computer. But in fact if you were to run this app on your device, you would see that even setting the font in IB is not working.

Your font is Special Elite. As you can see, I have it visible here in my running app:

Sample Image

Here's the code that I used:

    let lab = UILabel()
lab.text = "This is a test"
lab.font = UIFont(name:"SpecialElite-Regular", size:18)
lab.sizeToFit()
lab.frame.origin = CGPointMake(100,100)
self.view.addSubview(lab)

So you see, it is possible to refer to this font in code — if it is loaded properly. You are evidently not loading it properly. It's not in your app bundle, or it's not in the copy build phase, or it's not correctly listed in your Info.plist.

(Of course there's always a possibility that you're calling [UIFont fontWithName:size:] with a bad value for the name or for the size.)

Xcode 7.2.1 Custom font in Interface bulilder

Final Edit:

If you don't mind, I'll reword your question as I understand it much better now that you've kindly supplied me with your project.

In short your question is this:

My custom font does not work in my launch storyboard despite
the fact that it works elsewhere in the main app. How can I make it work in the launch screen?

It appears that you can't use custom fonts in a launch screen as they haven't yet been loaded. There are a number of answers on Stack Overflow confirming this. See here, here and here.

My advice would be to either avoid using the fonts in the storyboard or use them as an image.


Let's go through a number of the steps one-by-one to see if we can pinpoint the problem here.

Step 1. In your plist have you included the fonts under UIAppFonts aka Fonts provided by application? Like this:

Sample Image

Step 2. Also, have you made sure that the correct targets are selected? Like this:

Sample Image

Step 3. Also, try running this to see if your fonts are listed. If not then make sure they are spelled exactly as the filenames.

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSLog(@"Fonts: %@", [UIFont familyNames]);

Step 4. Another also - check the Font Book application on your mac, search for the font and find its correct name. Like this:

Sample Image

Step 5. Then check that the font is included in the bundle resources for the project like this:

Sample Image

Step 6. And finally double check that you selected to copy the fonts to the bundle when you added them to your project, like this:

Sample Image


Edit

It seems that the font works for you iff you refer to it in code but not if you refer to it using IB. So maybe...

Step 7. Delete the app from the simulator, clean the project and then rebuild.

Sample Image

Designing labels/text views with custom fonts in Interface Builder

Custom fonts can only be set programmatically..Unfortunately..this is termed as a bug in interface builder and hasn't been fixed yet.

Custom font seen in Interface builder but not in code in iOS project

You're going to kick yourself...

In your plist info file, you have:

`Quicksand-Dash.otf`

but the file you added is named:

`Quicksand_Dash.otf`

underscore not hyphen

Either rename your file, or edit the plist entry, and you'll see your font.

Debug console output:

family: Quicksand
name: Quicksand-BoldItalic
name: QuicksandDash-Regular
name: Quicksand-Bold

Here is a plain single-view project that works fine for me - on both Simulator and Device. (I have not added the fonts to OS X, only to my project):

https://github.com/DonMag/Quicksand

Just for setup reference:

Sample Image

Sample Image



Related Topics



Leave a reply



Submit