792

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?

6
  • 5
    In one of CS193p (iPhone Application Development) lectures in Stanford, Evan noted that installing your own font on a device is “lots of work”, which at least means it’s possible :-) Apr 24, 2009 at 11:17
  • 2
    iPhone 3.2 allows custom font, but it's iPad only (see answer below) Apr 11, 2010 at 7:06
  • 6
    I used images in the end. Apr 29, 2010 at 15:50
  • 2
    wow...after all of that, the answer is still images...unbelievable. I was really hoping for a solid implementation of custom fonts with the same capability as system fonts. alas...
    – d2burke
    Sep 17, 2012 at 19:22
  • 1
    I have substantially trimmed the original question to reflect the decade that has passed since I asked it. I'd encourage everyone else to trim their comments and answers appropriately. Jun 10, 2019 at 13:56

32 Answers 32

646

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.

13
  • 14
    Here's a step by step tutorial for iOS4: blog.beefyapps.com/2010/06/custom-fonts-in-ios-4
    – pm_labs
    Sep 20, 2010 at 13:34
  • 15
    This tutorial is very good. shang-liang.com/blog/custom-fonts-in-ios4 Importantly, the NSString argument to [UIFont fontWithName:...] is the OS name for the font rather than the file name.
    – Willster
    Jan 28, 2011 at 18:14
  • 153
    Before anyone else spends 3 hours installing fontforge in order to find the actual postscript name of the font required by iOS. I'll point out that you can simply press Cmd+I on the font in font book to find this information. Mar 20, 2012 at 16:51
  • 42
    After struggling to get the right font name, I just listed out the installed fonts and found it. Very helpful. Here's the code: for ( NSString *familyName in [UIFont familyNames] ) { NSLog(@"Family %@", familyName); NSLog(@"Names = %@", [UIFont fontNamesForFamilyName:familyName]); } Mar 29, 2012 at 20:05
  • 26
    If your code is not working, make sure you found font file appear in "Build Phases" -> "Copy Bundle Resouces"
    – Mickey
    Dec 20, 2012 at 9:55
299
+225

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

6
  • 2
    I've tried using your code but it crashes quite often depending on the font. For example, try using the African or Tiki fonts from here fontspace.com/category/tiki.
    – 4thSpace
    Jun 14, 2009 at 19:06
  • 2
    This library works great. I need help with the vertical spacing though. Can't figure out how to do it. So I have this message.numberOfLines = 3; How do I control the vertical spacing between line 1, and line 2 and line 3? Thank you, Tee
    – teepusink
    Nov 15, 2009 at 9:09
  • 2
    @commanda Hello commanda, the link you provided in your answer seems to be unavailable. Jan 28, 2013 at 6:36
  • 5
    @ParthBhatt You should use the solution provided in stackoverflow.com/a/2616101/21447 instead of FontLabel, since this functionality is now provided by CocoaTouch.
    – commanda
    Jan 28, 2013 at 22:52
  • 2
    @ParthBhatt: I don't know why the repo is no longer hosted at zynga, but it's available at github.com/kballard/fontlabel May 10, 2013 at 21:40
117

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
4
  • 3
    This seems to be supported back to 3.2, not just 4.0+ Oct 19, 2010 at 22:25
  • 1
    Thanks! The problem I've been having is that custom fonts seem to skew a bit high (extra space below) vs their built-in counterparts. I've tried using the FontLabel repo from GitHub, which helps some of the time, but not all of the time. Aug 1, 2011 at 14:58
  • 12
    Its worth noting that the string you pass to the UIFont constructor is NOT the filename minus the extension its the font's internal name. I had problems loading a font with a filename that I had shortened. When I used the entire font name as contained IN the file the font loaded fine.
    – slayton
    Aug 22, 2011 at 3:57
  • 5
    What slayton just said is critical. [UIFont fontWithName: expects the "Full name" of the font, which is visible by opening it up in Font Book and selecting: Preview --> Show Font Info Nov 20, 2011 at 8:16
51

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]];
0
36

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;
}
7
  • note to those wondering, this does work, but you'll need to call loadFonts right before using the font -- they don't seem to stay loaded throughout the application
    – pixel
    Jul 14, 2009 at 17:31
  • In OS 3.0 you definitely need to use the second second example, don't forget the import. Worked pretty well for me.
    – M. Ryan
    Nov 12, 2009 at 19:30
  • Another note -- this is a private framework, and dynamically loads it... both of which will likely stop the acceptance of your app into the AppStore.
    – pixel
    Dec 1, 2009 at 22:05
  • What is the conclusion? What alternatives to this method? Apr 14, 2010 at 12:36
  • 1
    SHould also note that [NSString sizeWithFont:[UIFont fontWithName:@"customFont" size:14]]; returns 0 width and height for any custom fonts I have tried. Jan 17, 2011 at 9:01
34

With iOS 8+ and Xcode 6+ you can make this easily. Here are the steps:

1) Drag and drop your font to Xcode Supporting Files folder. Don't forget to mark your app at Add to targets section. From this moment you can use this font in IB and choose it from font pallet.

enter image description here

2) To make this font available to in your device, open your info.plist and add Fonts provided by application key. It will contain Item 0 key, you must add your font name as the value. Font name can vary from your font file name. But first, try to add your filename in most cases this work.

enter image description here

If not, this article always helped me.

Here is swift snippet of the code from this article to help you find your font name.

func allFonts(){

   for family in UIFont.familyNames(){

       println(family)


       for name in UIFont.fontNamesForFamilyName(family.description)
       {
           println("  \(name)")
       }

   }

}

EDIT

I want to mention, that you need to add font files to your Target's Build Phases, Copy Bundle Resources. Without it, you won't see your font on the device. And it could lead to unexpected behaviour.

For example, I encounter a bug, when UITextField have custom font, but this font wasn't in the Copy Bundle Resources. And when I segue to the viewcontroller with this textfield, there is a delay about 4 seconds before viewDidLoad function was called. Resolving font troubles removed this delay. So, recommend to check it twice. (rdar://20028250) Btw, I wasn't able to reproduce the bug, but I'm sure that problem was with the font.

4
  • 1
    Just a note. Custom fonts are not available in LaunchScreens even if properly configured in IB. Which may be logic.
    – E. Rivera
    Apr 16, 2015 at 14:29
  • @Rivera wow, great note! Didn't know that. Possible alternative is to use UIImage with text instead, I think. Apr 17, 2015 at 18:48
  • @Rivera, thanks for that info- is it documented anywhere? I was going over the steps repeatedly thinking I missed a step. Jan 4, 2016 at 19:29
  • @spongessuck not sure about your question, but there is some docs developer.apple.com/library/ios/documentation/StringsTextFonts/… Jan 5, 2016 at 10:53
27

I have done this like this:

Load the font:

- (void)loadFont{
  // Get the path to our custom font and create a data provider.
  NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"mycustomfont" ofType:@"ttf"]; 
  CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);

  // Create the font with the data provider, then release the data provider.
  customFont = CGFontCreateWithDataProvider(fontDataProvider);
  CGDataProviderRelease(fontDataProvider); 
}

Now, in your drawRect:, do something like this:

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    // Get the context.
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);
    // Set the customFont to be the font used to draw.
    CGContextSetFont(context, customFont);

    // Set how the context draws the font, what color, how big.
    CGContextSetTextDrawingMode(context, kCGTextFillStroke);
    CGContextSetFillColorWithColor(context, self.fontColor.CGColor);
    UIColor * strokeColor = [UIColor blackColor];
    CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
    CGContextSetFontSize(context, 48.0f);

    // Create an array of Glyph's the size of text that will be drawn.
    CGGlyph textToPrint[[self.theText length]];

    // Loop through the entire length of the text.
    for (int i = 0; i < [self.theText length]; ++i) {
        // Store each letter in a Glyph and subtract the MagicNumber to get appropriate value.
        textToPrint[i] = [[self.theText uppercaseString] characterAtIndex:i] + 3 - 32;
    }
    CGAffineTransform textTransform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
    CGContextSetTextMatrix(context, textTransform);
    CGContextShowGlyphsAtPoint(context, 20, 50, textToPrint, [self.theText length]);
}

Basically you have to do some brute force looping through the text and futzing about with the magic number to find your offset (here, see me using 29) in the font, but it works.

Also, you have to make sure the font is legally embeddable. Most aren't and there are lawyers who specialize in this sort of thing, so be warned.

4
  • 1
    I'm sure it does work well, but I (really really) don't want to write my own layout engine to handle everything UILabel and UITextView currently do for me - particularly word wrapping, positioning, and editing. Dec 16, 2008 at 3:55
  • 1
    AFAICS, this only works with uppercase characters, hence the 'uppercaseString' call. Dec 8, 2009 at 21:47
  • 2
    I'm downvoting since the "magic number" trick is a pure hack that doesn't really work. The real solution is to get the cmap table from the TTF file. Dec 9, 2009 at 13:28
  • 1
    This is a neat piece of info on using fonts directly with Core Graphics. Just to update for future readers: In newer OSes, one could and should use Core Text. Alternatively, one could use CGContextShowTextAtPoint() -- although it doesn't work with UTF-8. Aug 24, 2012 at 13:34
21

Yes, you can include custom fonts. Refer to the documentation on UIFont, specifically, the fontWithName:size: method.

1) Make sure you include the font in your resources folder.

2) The "name" of the font is not necessarily the filename.

3) Make sure you have the legal right to use that font. By including it in your app, you're also distributing it, and you need to have the right to do that.

5
  • Have you actually done this successfull? I can't make it work, and googling finds only other people who have tried it and failed. Dec 12, 2008 at 2:12
  • Yes. I have. Again, make sure you're using the proper font name.
    – August
    Dec 12, 2008 at 4:15
  • 1
    When you say "The "name" of the font is not necessarily the filename" ... how do find the correct name? Feb 19, 2009 at 15:27
  • 7
    Yeah, this answer is wrong. You can't do this out of the box. Apr 11, 2010 at 5:03
  • iOS 9 update, UIFont docs don't mention anything about custom fonts
    – Cbas
    Jun 6, 2016 at 2:30
20

If you are using xcode 4.3, you have to add the font to the Build Phase under Copy Bundle Resources, according to https://stackoverflow.com/users/1292829/arne in the thread, Custom Fonts Xcode 4.3. This worked for me, here are the steps I took for custom fonts to work in my app:

  1. Add the font to your project. I dragged and dropped the OTF (or TTF) files to a new group I created and accepted xcode's choice of copying the files over to the project folder.
  2. Create the UIAppFonts array with your fonts listed as items within the array. Just the names, not the extension (e.g. "GothamBold", "GothamBold-Italic").
  3. Click on the project name way at the top of the Project Navigator on the left side of the screen.
  4. Click on the Build Phases tab that appears in the main area of xcode.
  5. Expand the "Copy Bundle Resources" section and click on "+" to add the font.
  6. Select the font file from the file navigator that pops open when you click on the "+".
  7. Do this for every font you have to add to the project.
0
14

I would recommend following one of my favorite short tutorials here: http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/ from which this information comes.

Step 1 - Drag your .ttf or .otf from Finder into your Project

NOTE - Make sure to click the box to 'Add to targets' on your main application target

Drag font files into your project and click to add them to your target

If you forgot to click to add it to your target then click on the font file in your project hierarchy and on the right side panel click the main app target in the Target Membership section

how to add fonts to app target if you forgot

To make sure your fonts are part of your app target make sure they show up in your Copy Bundle Resources in Build Phases

how to check resources to see if fonts are part of app target

Step 2 - Add the font file names to your Plist

Go to the Custom iOS Target Properties in your Info Section and add a key to the items in that section called Fonts provided by application (you should see it come up as an option as you type it and it will set itself up as an array. Click the little arrow to open up the array items and type in the names of the .ttf or .otf files that you added to let your app know that these are the font files you want available

NOTE - If your app crashes right after this step then check your spelling on the items you add here

example plist with custom fonts added

Step 3 - Find out the names of your fonts so you can call them

Quite often the font name seen by your application is different from what you think it is based on the filename for that font, put this in your code and look over the log your application makes to see what font name to call in your code

Swift

for family: String in UIFont.familyNames(){
  print("\(family)")
  for names: String in UIFont.fontNamesForFamilyName(family){
      print("== \(names)")
  }
}

Objective C

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

Your log should look something like this:

Example of searching log to find font names

Step 4 - Use your new custom font using the name from Step 3

Swift

 label.font = UIFont(name: "SourceSansPro-Regular", size: 18)

Objective C

 label.font = [UIFont fontWithName:@"SourceSansPro-Regular" size:18];
0
12

Here's the step by step instructions how to do it. No need extra library or any special coding.

http://shang-liang.com/blog/custom-fonts-in-ios4/

Most of the time the issue is with the font not the method. The best way to do it is to use a font that for sure will work, like verdana or geogia. Then change to the intended font. If it does not work, maybe the font name is not right, or the font is not a well formated font.

0
12

It is very easy to add a new font on your existing iOS App.

You just need to add the font e.g. font.ttf into your Resource Folder.

Open your application info.plist. Add a new row as "Fonts provided by application" and type the font name as font.ttf.

And when setting the font do as setFont:"corresponding Font Name"

You can check whether your font is added or not by NSArray *check = [UIFont familyNames];.

It returns all the font your application support.

10

Find the TTF in finder and "Get Info". Under the heading "Full name:" it gave me a name which I then used with fontWithName (I just copied and pasted the exact name, in this case no '.ttf' extension was necessary).

0
8

It's not out yet, but the next version of cocos2d (2d game framework) will support variable length bitmap fonts as character maps.

http://code.google.com/p/cocos2d-iphone/issues/detail?id=317

The author doesn't have a nailed down release date for this version, but I did see a posting that indicated it would be in the next month or two.

8

One important notice: You should use the "PostScript name" associated with the font, not its Full name or Family name. This name can often be different from the normal name of the font.

7

follow this step

1)Copy your font in your project

2)open your .plist file in source code mode...(Note- Dont open info.plist)

3)Before that - Right click on your font and open it in fontforge or similar editor and install it in your system ,It should be install

4)Type this

 <key>UIAppFonts</key>
<array>
    <string>MyriadPro.otf</string>
</array>

5)Type this code in your class .m

 [lblPoints setFont:[UIFont fontWithName:@"Myriad Pro" size:15.0]];

Here lblPoints will be change as of your UILabel

Done!! If still your font not work,check your fonts compatibility first

6
  • 1
    why don't open info.plist ?
    – Raptor
    Aug 12, 2013 at 7:58
  • why space in between Myraid & Pro in 5th point when there is none in Plist in point 4? Apr 24, 2020 at 5:25
  • @jayantrawat Its font name which is shown in the system after we install the font, and .plist is the name of font file[ .ttl or otf ] May 1, 2020 at 4:17
  • @jayantrawat if my solution helps then request to you give upvote on the answer so that other users can take help the same way May 2, 2020 at 6:43
  • @KirtikumarA. I already did it using a tutorial ..I only had a spelling mistake in my font name..I was just curios why you had a space in 5th point in font name while in 4th point in plist there is no space..anyhow still upvoted your answer for replying which is so rare now a days.. May 3, 2020 at 16:44
6

Maybe the author forgot to give the font a Mac FOND name?

  1. Open the font in FontForge then go to Element>Font Info
  2. There is a "Mac" Option where you can set the FOND name.
  3. Under File>Export Font you can create a new ttf

You could also give the "Apple" option in the export dialog a try.

DISCLAIMER: I'm not a IPhone developer!

6

I have been trying out the various suggestions on this page on iOS 3.1.2 and these are my conclusions:

Simply using [UIFont fontWithName:size:] with the fonts in the Resources directory will not work, even if the FOND name is set using FontForge.

[UIFont fontWithName:size:] will work if the fonts are loaded first using GSFontAddFromFile. But GSFontAddFromFile is not part of iOS 3.1.2 so it has to be dynamically loaded as described by @rpetrich.

0
5

Look up ATSApplicationFontsPath

A simple plist entry that allows you to include the font file(s) in your app resources folder and they "just work" in your app.

2
  • Have you tried this on device? I had no joy, and googling suggests that this plist entry isn't supported on iPhone, even though it's documented for iPhone OS. Apr 9, 2009 at 13:53
  • 1
    Sorry, I am only using it in an OS X app at the moment. Apr 9, 2009 at 15:48
5

I've combined some of the advice on this page into something that works for me on iOS 5.

First, you have to add the custom font to your project. Then, you need to follow the advice of @iPhoneDev and add the font to your info.plist file.

After you do that, this works:

UIFont *yourCustomFont = [UIFont fontWithName:@"YOUR-CUSTOM-FONT-POSTSCRIPT-NAME" size:14.0];
[yourUILabel setFont:yourCustomFont];

However, you need to know the Postscript name of your font. Just follow @Daniel Wood's advice and press command-i while you're in FontBook.

Then, enjoy your custom font.

5

First add the font in .odt format to your resources, in this case we will use DINEngschriftStd.otf, then use this code to assign the font to the label

[theUILabel setFont:[UIFont fontWithName:@"DINEngschriftStd" size:21]];

To make sure your font is loaded on the project just call

NSLog(@"Available Font Families: %@", [UIFont familyNames]);

On the .plist you must declare the font. Just add a 'Fonts provided by application' record and add a item 0 string with the name of the font (DINEngschriftStd.otf)

5

For iOS 3.2 and above: Use the methods provided by several above, which are:

  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. Use [UIFont fontWithName:@"Real Font Name" size:16] in your application.

BUT The "Real Font Name" is not always the one you see in Fontbook. The best way is to ask your device which fonts it sees and what the exact names are.

I use the uifont-name-grabber posted at: uifont-name-grabber

Just drop the fonts you want into the xcode project, add the file name to its plist, and run it on the device you are building for, it will email you a complete font list using the names that UIFont fontWithName: expects.

5

Better solution is to add a new property "Fonts provided by application" to your info.plist file.

Then, you can use your custom font like normal UIFont.

3

There is a new way to use custom fonts, starting with iOS 4.1. It allows you to load fonts dynamically, be they from files included with the app, downloaded data, or what have you. It also lets you load fonts as you need them, whereas the old method loads them all at app startup time, which can take too long if you have many fonts.

The new method is described at ios-dynamic-font-loading

You use the CTFontManagerRegisterGraphicsFont function, giving it a buffer with your font data. It's then available to UIFont and web views, just as with the old method. Here's the sample code from that link:

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);
2
  • I have used this code but get the &error Failed to load font: The operation couldn’t be completed. (com.apple.coretext error 105 - Could not register the CGFont '<CGFont (0x14fe7df0): Shruti>')
    – Hiren
    Apr 10, 2013 at 12:38
  • According to the documentation, error 105 is kCTFontManagerErrorAlreadyRegistered, "The file has already been registered in the specified scope." developer.apple.com/library/ios/#documentation/Carbon/Reference/…
    – David M.
    Apr 21, 2013 at 20:05
3

Swift, code way: (works also with swift 2.0)

Add the required fonts to your project (just like adding images, just drag to Xcode), make sure that they are targeted to your project
add this method and load custom fonts (recommended in appDelegate didFinishLaunchingWithOptions)

func loadFont(filePath: String) {

    let fontData = NSData(contentsOfFile: filePath)!

    let dataProvider = CGDataProviderCreateWithCFData(fontData)
    let cgFont = CGFontCreateWithDataProvider(dataProvider)!

    var error: Unmanaged<CFError>?
    if !CTFontManagerRegisterGraphicsFont(cgFont, &error) {
        let errorDescription: CFStringRef = CFErrorCopyDescription(error!.takeUnretainedValue())
        print("Unable to load font: %@", errorDescription, terminator: "")
    }

}

Use example:

if let fontPath = NSBundle.mainBundle().pathForResource("My-Font", ofType: "ttf"){
      loadFont(fontPath)
}

Use the font:

UIFont(name: "My-Font", size: 16.5)
1
  • Just wondering, how come you won't just add the font in your Info.plist seems like a lot more work for essentially the same thing. Aug 4, 2016 at 14:55
2

You can add the required "FONT" files within the resources folder. Then go to the Project Info.plist file and use the KEY "Fonts provided by the application" and value as "FONT NAME".

Then you can call the method [UIFont fontwithName:@"FONT NAME" size:12];

1
  • File name, not font name...
    – Hoshi
    May 24, 2023 at 19:37
1

I made everything possible but the new fonts dont appear so I found the solution:

When you drag the fot files(otf or ttf) DONT forget to check the checkbox under "Add to targets".

After doing that your font will appear and everything will work fine.

1

Although some of the answers above are correct, I have written a detailed visual tutorial for people still having problems with fonts.

The solutions above which tell you to add the font to the plist and use

[self.labelOutlet setFont:[UIFont fontWithName:@"Sathu" size:10]];

are the correct ones. Please do now use any other hackish way. If you are still facing problems with finding font names and adding them, here is the tutorial -

Using custom fonts in ios application

2
  • I just tried this. Seems to work but my font is tiny, no matter what size I set it. For what it's worth, I'm using a freely available OTF file: philsfonts.com/index.php/free_fonts
    – ari gold
    Jun 9, 2013 at 2:07
  • Try it with any other custom font and if problem persists, let me know. If it works that way, then there is a problem with your font. Jun 26, 2013 at 18:54
1

yes you can use custom font in your application

step by step following there:

Add your custom font files into your project in supporting files

Add a key to your Info.plist file called UIAppFonts.

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:@"your Custom font Name" size:20] to get the custom font to use with your UILabels after applying this if your not getting correct font then you double click on the custom font , and see carefully top side font name is comming and copy this font , paste, here [UIFont fontWithName:@" here past your Custom font Name" size:20]

i hope you will get correct answer

1

yes you can use custom font in your application

step by step following there:

  • Add your custom font files into your project in supporting files
  • Add a key to your Info.plist file called UIAppFonts.
  • 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:@"your Custom font Name" size:20] to get the custom font to use with your UILabels

after applying this if your not getting correct font then you double click on the custom font , and see carefully top side font name is comming and copy this font , paste, here [UIFont fontWithName:@" here past your Custom font Name" size:20] i hope you will get correct answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.