Custom font in a storyboard?

IphoneIosCocoa TouchStoryboardCustom Font

Iphone Problem Overview


I have a font added to my iOS project. It's in the project and copied when the project builds. It appears to be a valid font and will show up if I list all fonts on the device through the app. I have the plist set up properly to include the font. I can't seem to get the find to show up to use it in my text controls, buttons, etc in Xcode 4.2's storyboard section. Further it doesn't appear to allow me to type the font name, it forces me to use the font dialog. I attempted to install this font in the system as well, but cannot seem to get it to show in this list. Do I need to do this in code only or can I do this through the storyboard interface?

Note that I can get this working in code, but it would be much more convenient to do this via the storyboard.

Iphone Solutions


Solution 1 - Iphone

UPDATE: Xcode 6 Interface Builder now allows you to select custom fonts and will render them at design time correctly.

I know this question is quite old, but I've been struggling to find an easy way to specify a custom font in Storyboard (or Interface Builder) easily for iOS 5 and I found a quite convenient solution.

First, make sure that you've added the font to the project by following this tutorial. Also remember that UINavigationBar, UITabBar and UISegmentedControl custom font can be specified by using the setTitleTextAttributes: method of the UIAppearance proxy.

Add the categories below to the project for UIButton, UITextField, UILabel and any other component that needs a custom font. The categories simply implement a new property fontName which changes the current font of the element while maintaining the font size.

To specify the font in Storyboard, just select the desired element (label, button, textview, etc) and add a User Defined Runtime Attribute with Key Path set to fontName of type String and value with the name of your custom font.

Custom font Storyboard

And that's it, you don't even need to import the categories. This way you don't need an outlet for every UI component that requires a custom font and you don't need to code it manually.

Take into account that the font won't show in Storyboard, but you will see it when running on your device or simulator.


Category files

UIButton+TCCustomFont.h:

#import <UIKit/UIKit.h>

@interface UIButton (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end

UIButton+TCCustomFont.m:

#import "UIButton+TCCustomFont.h"

@implementation UIButton (TCCustomFont)

- (NSString *)fontName {
    return self.titleLabel.font.fontName;
}

- (void)setFontName:(NSString *)fontName {
    self.titleLabel.font = [UIFont fontWithName:fontName size:self.titleLabel.font.pointSize];
}

@end

UILabel+TCCustomFont.h:

#import <UIKit/UIKit.h>

@interface UILabel (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end

UILabel+TCCustomFont.m:

#import "UILabel+TCCustomFont.h"

@implementation UILabel (TCCustomFont)

- (NSString *)fontName {
    return self.font.fontName;
}

- (void)setFontName:(NSString *)fontName {
    self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}

@end

UITextField+TCCustomFont.h:

#import <UIKit/UIKit.h>

@interface UITextField (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end

UITextField+TCCustomFont.m:

#import "UITextField+TCCustomFont.h"

@implementation UITextField (TCCustomFont)

- (NSString *)fontName {
    return self.font.fontName;
}

- (void)setFontName:(NSString *)fontName {
    self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}

@end

Also downloadable from GIST and also as a single file.

Troubleshooting

If you run against runtime errors because the fontName property is not specified just add the flag -all_load under Other linker Flags in project settings to force the linker to include the categories.

Solution 2 - Iphone

From Xcode6.0 , as Xcode6.0 release note:

>Interface Builder renders embedded custom iOS fonts during design time, giving a more accurate preview of how the finished app will look, with correct dimensions.

you can set label font in storyboard.You can do as follow

  1. Get you custom font file(.ttf,.ttc)

  2. Import the font files to your Xcode project

    enter image description here

  3. In the app-info.plist,add a key named Fonts provided by application.It's an array type , add all your font file name to the array,note:including the file extension.

enter image description here

  1. In the storyboard , drag a UILabel to you interface,select the label , and navigate to the Attribute Inspector,click the right icon button of the Font select area.In the popup panel , choose Font to Custom, and choose the Family of you embeded font name.

enter image description here

ref to

Solution 3 - Iphone

This is a bug in XCode, been there since 3.x and still not fixed. I too have faced the same issue, even tried adding it to my system with no luck. Here is another SO post about it https://stackoverflow.com/questions/3398693/fonts-not-displaying-in-interface-builder

Solution 4 - Iphone

Monjer's answer is the correct one. If you, like me, don't see the added font after compiling, make sure to add the font's to the build phases (targets -> build phases -> copy bundle resources -> add your font files)

Solution 5 - Iphone

As of XCode 9.4, XCode still does not respect fonts directly in the storyboard. You can see them at design time, but not at runtime. If you are using fontWithName API, you get to know it returns nil, but when used in storyboard/xib, there is no way to know why it doesn't appear.

  • The only way to make sure it works readily from Storyboard/XIB at runtime is to add .ttc/.ttf into Copy Bundle Resources Build Phase.

  • As of 9.4, adding font file names to info.plist seems no longer a requirement.

Solution 6 - Iphone

redent84's solution was awesome!

I'd just add an improvement, add the category on NSObject, so you only have to do it once.

NSObject+CustomFont.h

#import <Foundation/Foundation.h>

@interface NSObject (CustomFont)

@property (strong, nonatomic) NSString *fontName;
@property (strong, nonatomic) UIFont *font;

@end

NSObject+CustomFont.m

#import "NSObject+CustomFont.h"

@implementation NSObject (CustomFont)

@dynamic font;

- (NSString *)fontName
{
    return self.font.fontName;
}

- (void)setFontName:(NSString *)fontName
{
    self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}

@end

Solution 7 - Iphone

None of these worked for me, but this did.

#import <UIKit/UIKit.h>

@interface UILabelEx : UILabel


@end

#import "UILabelEx.h"
#import "Constants.h"

@implementation UILabelEx

- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {
    [super traitCollectionDidChange: previousTraitCollection];
    
    self.font = [UIFont fontWithName:APP_FONT size:self.font.pointSize];   
}
@end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionslycrelView Question on Stackoverflow
Solution 1 - Iphoneredent84View Answer on Stackoverflow
Solution 2 - IphonemonjerView Answer on Stackoverflow
Solution 3 - IphoneDanView Answer on Stackoverflow
Solution 4 - IphoneHugglesNLView Answer on Stackoverflow
Solution 5 - IphoneNirav BhattView Answer on Stackoverflow
Solution 6 - IphoneRodrigo RuizView Answer on Stackoverflow
Solution 7 - IphoneJulesView Answer on Stackoverflow