You can use UIFontWDCustomLoader category to load any compatible font into your iOS projects at runtime without messing with plist, font unknown names or strange magic.
The only things you'll have to know are your font filenames and this library name.
You can also use this library to load new fonts after app installation.
- Drag'n drop your font into Xcode project, selecting "Add to Targets" when prompted.
- Check "Target Membership" of your added font files (File inspector on the right).
If you can't see a font, check under Build Phases > Copy Bundle Resource
: you must see the font filename listed here.
#import "UIFont+WDCustomLoader.h"
/* FONT COLLECTION FILE (TTC OR OTC) */
// Create an NSURL for your font file: 'Lao MN.ttc'
NSURL *laoFontURL = [[NSBundle mainBundle] URLForResource:@"Lao MN" withExtension:@"ttc"]];
// Do the registration.
NSArray *fontPostScriptNames = [UIFont registerFontFromURL:laoFontURL];
// If everything went ok, fontPostScriptNames will become @[@"LaoMN",@"LaoMN-Bold"]
// and collection will be registered.
// (Note: On iOS < 7.0 you will get an empty array)
// Then, anywhere in your code, you can do
UIFont *laoFont = [UIFont fontWithName:@"LaoMN" size:18.0f];
or
/* SINGLE FONT FILE (TTF OR OTF) */
// Create an NSURL for your font file: 'Lato-Hairline.ttf'
NSURL *latoHairlineFontURL = [[NSBundle mainBundle] URLForResource:@"Lato-Hairline" withExtension:@"ttf"]];
// Do the registration.
NSArray *fontPostScriptNames = [UIFont registerFontFromURL:latoHairlineFontURL];
// If everything went ok, fontPostScriptNames will become @[@"Lato-Hairline"]
// and collection will be registered.
// Then, anywhere in your code, you can do
UIFont *latoHairlineFont = [UIFont fontWithName:@"Lato-Hairline" size:18.0f];
// or
UIFont *latoHairlineFont = [UIFont customFontWithURL:latoHairlineFontURL size:18.0f];
// or (*deprecated*)
UIFont *myCustomFont = [UIFont customFontOfSize:18.0f withName:@"Lato-Hairline" withExtension:@"ttf"];
/* SINGLE FONT (TTF OR OTF) */
// Create an NSURL for your font file: 'Lato-Hairline.ttf'
NSURL *latoHairlineFontURL = [[NSBundle mainBundle] URLForResource:@"Lato-Hairline" withExtension:@"ttf"]];
// Then, anywhere in your code, you can do
UIFont *latoHairlineFont = [UIFont customFontWithURL:latoHairlineFontURL size:18.0f];
// or (*deprecated*)
UIFont *myCustomFont = [UIFont customFontOfSize:18.0f withName:@"Lato-Hairline" withExtension:@"ttf"];
NOTE: Font registration will be made on first [ UIFont customFont… ]
method call.
UIFontWDCustomLoader requires:
- ARC
- Deployment target greater or equal to iOS 4.1
- CoreText Framework
This library has been tested with: iOS 5, 6 and 7
Simply download it from here and include it in your project manually.
You have the canonical git submodule
option. Simply issue
git submodule add https://github.com/daktales/UIFontWDCustomLoader.git <path>
in your root folder of your repository.
You can use CocoaPods to install this library.
In your Xcode project folder create (or modify) a 'podfile' file with:
platform :ios
pod 'UIFontWDCustomLoader', '~> 0.1.0'
Then launch pod install
in the same folder (using console).
Now you must open your project by .workspace
file.
This code is distributed under the terms and conditions of the MIT license.
The entire idea behind this library came after I see how FlatUIKit loads its fonts. So a big thanks to them. Link