Elefont
Load fonts in your iOS app without any hassle
About
If:
- you want an effortless way of adding and using custom fonts in your iOS projects
or - you want to load fonts from your custom Bundle(s)
or - you want to load and use fonts not bundled with the app
then Elefont is what you need.
Features
- Imports fonts from
Bundle.main
- Imports fonts from custom Bundle(s)
- Imports fonts from any local URL or path
- CocoaPods Support
Installation
CocoaPods
pod 'Elefont'
Manual
- Download Elefont.
- Copy
Sources/Elefont.swift
into your project.
Usage
A typical custom font integration without Elefont goes like this:
- Include your fonts to the Xcode project;
- Create a list with those fonts and update the
Info.plist
file; - Use the fonts in the
Storyboard
/Xib
or programmatically with
UIFont(name: "<font-postscript-name>", size: <size>)
If you've integrated custom fonts in your iOS projects (I'm assuming you did given that you looked for a better solution here), then you know what a hassle step 2 (ex: wrong font file names) and step 3 (wrong font PostScript names, which is not the font file names).
Luckily, Elefont solves those two annoyances with this line:
Elefont.eat()
which will load all the fonts in Bundle.main
, completely removing step 2 and providing the PostScript names for the loaded fonts.
But what if your fonts are in a custom Bundle? Well, no problem:
Elefont.eat(bundle: <custom_bundle_object>)
I've mentioned earlier that Elefont can load fonts from local URL or path. Here's how you can do that:
Elefont.eat(at:)
// or
Elefont.eat(atPath:)
Each of the methods above can be called with a completion handler, in case you need a list of all of the loaded fonts generated by the load operation:
Elefont.eat(/* arguments */) { fonts in
print(fonts)
}
If you need to debug font loading, set debugEnabled = true
:
Elefont.debugEnabled = true
Public API
Below are all the possible variations of Elefont usage (fonts
is an array of Strings containing PostScript font names):
// 1. Load from main Bundle without callback
Elefont.eat()
// 2. Load from main Bundle with callback
Elefont.eat { fonts in
print(fonts)
}
// 3. Load from custom Bundle without callback
Elefont.eat(bundle:)
// 4. Load from custom Bundle with callback
Elefont.eat(bundle:) { fonts in
print(fonts)
}
// 5. Load from local URL without callback
Elefont.eat(at:)
// 6. Load from local URL with callback
Elefont.eat(at:) { fonts in
print(fonts)
}
// 7. Load from local path without callback
Elefont.eat(atPath:)
// 8. Load from local path with callback
Elefont.eat(atPath:) { fonts in
print(fonts)
}
Demo Project
You can use the ElefontDemo/ElefontDemo.xcodeproj project to see Elefont for loading fonts from main Bundle and from a URL path. Press "Load fonts" button to see the texts update with their corresponding fonts.