Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically load a font under iOS. (for real)

I've seen this question asked and answered numerous times, but I haven't seen a real answer. The common "solutions" are:

  • Add the font to the application bundle and register it in the info.plist file.
  • Use a custom font parsing and rendering library (like the Zynga's FontLabel).
  • It cannot be done.

So the question is: How to dynamically load a font under iOS? Loading the font "dynamically" means loading any given font which is not known at the time of the app's compilation.

like image 439
Ark-kun Avatar asked Dec 27 '12 02:12

Ark-kun


People also ask

How do I install a dynamic font?

WebFont. load({ google: { families: ['Droid Sans'] }, timeout:5000, fontactive: function(familyName,fvd){ //This is called once font has been rendered in browser // Your business logic goes here }, }); You can also load custom fonts apart from google fonts.

How do I import fonts to iOS?

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.

How use OTF font in iOS?

Add the Font File to Your Xcode Project To add a font file to your Xcode project, select File > Add Files to “Your Project Name” from the menu bar, or drag the file from Finder and drop it into your Xcode project. You can add True Type Font (. ttf) and Open Type Font (. otf) files.

What is iOS dynamic Type?

Dynamic Type is an iOS feature that scales font according to the user settings. More than 40% of iOS users have changed the default system font size. Both larger and smaller. The easiest way to support Dynamic Type is to use System Font Styles either from Storyboard or programmatically with UIKit.


2 Answers

Fonts can easily be dynamically loaded from any location or any byte stream. See the article here: http://www.marco.org/2012/12/21/ios-dynamic-font-loading

NSData *inData = /* your font-file data */;
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
    CFStringRef errorDescription = CFErrorCopyDescription(error)
    NSLog(@"Failed to load font: %@", errorDescription);
    CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);
  • You don't have to put the font in your bundle.
  • You don't have to explicitly register the font in your info.plist.

See also: https://developer.apple.com/library/mac/#documentation/Carbon/Reference/CoreText_FontManager_Ref/Reference/reference.html#//apple_ref/doc/uid/TP40008278

https://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGFont/Reference/reference.html#//apple_ref/c/func/CGFontCreateWithDataProvider

like image 168
Ark-kun Avatar answered Oct 06 '22 16:10

Ark-kun


Great time for a recent post from Marco titled Loading iOS fonts dynamically.

NSData *inData = /* your decrypted font-file data */;
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
    CFStringRef errorDescription = CFErrorCopyDescription(error)
    NSLog(@"Failed to load font: %@", errorDescription);
    CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);
like image 38
shawnwall Avatar answered Oct 06 '22 17:10

shawnwall