ios - Can I embed a custom font in an iPhone application?

ID : 2205

viewed : 151

Tags : ioscocoa-touchfontsios





Top 5 Answer for ios - Can I embed a custom font in an iPhone application?

vote vote

100

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)

  1. Add your custom font files into your project using Xcode as a resource
  2. Add a key to your Info.plist file called UIAppFonts.
  3. Make this key an array
  4. For each font you have, enter the full name of your font file (including the extension) as items to the UIAppFonts array
  5. Save Info.plist
  6. Now in your application you can simply call [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.

vote vote

84

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

vote vote

71

There is a simple way to use custom fonts in iOS 4.

  1. Add your font file (for example, Chalkduster.ttf) to Resources folder of the project in XCode.
  2. Open info.plist and add a new key called UIAppFonts. The type of this key should be array.
  3. Add your custom font name to this array including extension (Chalkduster.ttf).
  4. Now you can use [UIFont fontWithName:@"Chalkduster" size:16] in your application.

Unfortunately, IB doesn't allow to initialize labels with custom fonts. See this question to solve this problem. My favorite solution is to use custom UILabel subclass:

@implementation CustomFontLabel  - (id)initWithCoder:(NSCoder *)decoder {     if (self = [super initWithCoder: decoder])     {         [self setFont: [UIFont fontWithName: @"Chalkduster" size: self.font.pointSize]];     }     return self; }  @end 
vote vote

70

In Info.plist add the entry "Fonts provided by application" and include the font names as strings:

Fonts provided by application            Item 0        myfontname.ttf            Item 1        myfontname-bold.ttf            ... 

Then check to make sure your font is included by running :

for (NSString *familyName in [UIFont familyNames]) {     for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {          NSLog(@"%@", fontName);     } } 

Note that your ttf file name might not be the same name that you use when you set the font for your label (you can use the code above to get the "fontWithName" parameter):

[label setFont:[UIFont fontWithName:@"MyFontName-Regular" size:18]]; 
vote vote

53

edit: This answer is defunct as of iOS3.2; use UIAppFonts

The only way I've been able to successfully load custom UIFonts is via the private GraphicsServices framework.

The following will load all the .ttf fonts in the application's main bundle:

BOOL GSFontAddFromFile(const char * path); NSUInteger loadFonts() {     NSUInteger newFontCount = 0;     for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"ttf" inDirectory:nil])         newFontCount += GSFontAddFromFile([fontFile UTF8String]);     return newFontCount; } 

Once fonts are loaded, they can be used just like the Apple-provided fonts:

NSLog(@"Available Font Families: %@", [UIFont familyNames]); [label setFont:[UIFont fontWithName:@"Consolas" size:20.0f]]; 

GraphicsServices can even be loaded at runtime in case the API disappears in the future:

#import <dlfcn.h> NSUInteger loadFonts() {     NSUInteger newFontCount = 0;     NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.apple.GraphicsServices"];     const char *frameworkPath = [[frameworkBundle executablePath] UTF8String];     if (frameworkPath) {         void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY);         if (graphicsServices) {             BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, "GSFontAddFromFile");             if (GSFontAddFromFile)                 for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"ttf" inDirectory:nil])                     newFontCount += GSFontAddFromFile([fontFile UTF8String]);         }     }     return newFontCount; } 

Top 3 video Explaining ios - Can I embed a custom font in an iPhone application?







Related QUESTION?