I would like to have an app include a custom font for rendering text, load it, and then use it with standard UIKit
elements like UILabel
. Is this possible?
You can download fonts from the App Store app , then use them in documents you create on iPhone. After you download an app containing fonts from the App Store, open the app to install the fonts. To manage installed fonts, go to Settings > General, then tap Fonts.
Open the file you want to embed fonts in. On the application (PowerPoint or Word) menu, select Preferences. In the dialog box, under Output and Sharing, select Save. Under Font Embedding, select Embed fonts in the file.
iOS 3.2 and later support this. Straight from the What's New in iPhone OS 3.2 doc:
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.
There is an ongoing thread on Apple Developer Forums:
https://devforums.apple.com/thread/37824 (login required)
And here's an excellent and simple 3 steps tutorial on how to achieve this (broken link removed)
Info.plist
file called UIAppFonts
.UIAppFonts
arrayInfo.plist
[UIFont fontWithName:@"CustomFontName" size:12]
to get the custom font to use with your UILabels and UITextViews, etc…Also: Make sure the fonts are in your Copy Bundle Resources.
Edit: As of iOS 3.2, this functionality is built in. If you need to support pre-3.2, you can still use this solution.
I created a simple module that extends UILabel
and handles loading .ttf files. I released it opensource under the Apache license and put it on github Here.
The important files are FontLabel.h
and FontLabel.m
.
It uses some of the code from Genericrich's answer.
Browse the source Here.
OR
Copy your font file into resources
Add a key to your Info.plist
file called UIAppFonts. ("Fonts provided by application)
Make this key an array
For each font you have, enter the full name of your font file (including the extension) as items to the UIAppFonts array
Save Info.plist
Now in your application you can simply call [UIFont fontWithName:@"CustomFontName" size:15]
to get the custom font to use with your UILabels
and UITextViews
, etc…
For More Information
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With