Home » iPad » iPhone » How to include ttf fonts to iOS app

How to include ttf fonts to iOS app

Up till now there hasn’t been an easy way to add custom fonts to your iPhone applications. As of iOS 4 it has become very easy to do. Here is what you need to do in order to add custom fonts:

  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…

OR

add your font to your project and next function, and perform from start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#import <dlfcn.h>

// includer for font
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)
               {
                   
                    BOOL verizon = NO;
                   
                    NSLog(@"%@",[[UIDevice currentDevice] machine]);
                   
                    if ([[[UIDevice currentDevice] machine] rangeOfString:@"iPhone3,3"].location != NSNotFound) {
                         verizon = YES;
                    }
                   
                    for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"ttf" inDirectory:nil])
                    {
                         if ([fontFile rangeOfString:@"_"].location != NSNotFound && verizon) {
                              newFontCount += GSFontAddFromFile([fontFile UTF8String]);
                         }
                         
                         if ([fontFile rangeOfString:@"-"].location != NSNotFound && !verizon) {
                              newFontCount += GSFontAddFromFile([fontFile UTF8String]);
                         }

                    }
               }
          }
     }
     
     return newFontCount;
}

It’s that simple!

This entry was posted in iPad, iPhone and tagged , , , , , . Bookmark the permalink.

Comments are closed.