Custom Font Support
Applications that want to use custom fonts can now include those fonts in their application bundle and register those fonts with the system by including the UIAppFonts key in their Info.plist file. The value of this key is an array of strings identifying the font files in the application’s bundle. When the system sees the key, it loads the specified fonts and makes them available to the application.
Once the fonts have been set in the Info.plist
, you can use your custom fonts as any other font in IB or programatically.
Info.plist
and add Fonts provided by application key
(UIAppFonts). Add font name as the value to the Item 0 key. Note: Font name can vary from your font file name.
[Swift 3]
for family in UIFont.familyNames {
print("\(family)")
for name in UIFont.fontNames(forFamilyName: family) {
print(" \(name)")
}
}
[Objective - C]
for (NSString *familyName in [UIFont familyNames]){
NSLog(@"Family name: %@", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(@"--Font name: %@", fontName);
}
}
Custom Fonts for UI components from storyboard can be easily achieved with User Defined Runtime Attributes in storyboard and Categories.
The advantages are like,
Steps to follow
Font File: Add the Font file (.ttf) to the application bundle and add the entry for the font in Info.plist under Font provided by application as in this documentation of custom fonts.
Define Categories: Add a file like UIKit+IBExtensions and add the categories for UI elements like UILabel, UIButton etc. for which you want to set custom font. All the categories will be having a custom property say fontName. This will be using from the storyboard later for setting custom font (as in step 4).
#import <UIKit/UIKit.h>
//Category extension for UILabel
@interface UILabel (IBExtensions)
@property (nonatomic, copy) NSString *fontName;
@end
// Category extension for UITextField
@interface UITextField (IBExtensions)
@property (nonatomic, copy) NSString *fontName;
@end
// Category extension for UIButton
@interface UIButton (IBExtensions)
@property (nonatomic, copy) NSString *fontName;
@end
- Getters and Setters: Define getters and setters for the fontName property towards each category added.
#import "UIKit+IBExtensions.h"
@implementation UILabel (IBExtensions)
- (NSString *)fontName {
return self.font.fontName;
}
- (void)setFontName:(NSString *)fontName {
self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}
@end
@implementation UITextField (IBExtensions)
- (NSString *)fontName {
return self.font.fontName;
}
- (void)setFontName:(NSString *)fontName {
self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}
@end
@implementation UIButton (IBExtensions)
- (NSString *)fontName {
return self.titleLabel.font.fontName;
}
- (void)setFontName:(NSString *)fontName{
self.titleLabel.font = [UIFont fontWithName:fontName size:self.titleLabel.font.pointSize];
}
@end
- Setting font in storyboard: Add an entry in User Defined Runtime Attributes with fontName as keyPath and your Custom Font's Name as value with type as String as shown.
This will set your custom font while running the app.
Notes:
The following example shows how to apply custom fonts to a Navigation Bar and includes fixes for some quirky behaviors found in Xcode. One also may apply the custom fonts to any other UIControls such as UILabels, UIButtons, and more by using the attributes inspector after the custom font is added to the project. Please note the external links to working samples and videos near the bottom.
(You will likely need to toggle the Bar Tint for the Navigation Bar before Xcode picks up the new font)
Verified that this does work on Xcode 7.1.1+. (See the Samples below)
Some of these are repeated which means they are very likely worth noting.
Note ~ A nice checklist can be found from the Code With Chris website and you can see the sample download project.
If you have your own font and want to use that in your storyboard, then there is a decent set of answers on the following SO Question. One answer identifies these steps.
So Xcode naturally looks like it can handle custom fonts on UINavigationItem but that feature is just not updating properly (The font selected is ignored).
To workaround this:
One way is to fix using the storyboard and adding a line of code: First add a UIView (UIButton, UILabel, or some other UIView subclass) to the View Controller (Not the Navigation Item...Xcode is not currently allowing one to do that). After you add the control you can modify the font in the storyboard and add a reference as an outlet to your View Controller. Just assign that view to the UINavigationItem.titleView. You could also set the text name in code if necessary. Reported Bug (23600285).
@IBOutlet var customFontTitleView: UIButton!
//Sometime later...
self.navigationItem.titleView = customFontTitleView
Note - This example is derived from an answer I posted on SO (here).