custom fonts in ios 7

custom fonts in ios 7  using -'ios,iphone,fonts'

I'm developing a game and I would like to use a custom font in my app. I'm using SpriteKit for it, if that's important. I've tried using this but I cannot get it to work with this font

I've tried various things, and I've registered the font on the info.plist of the app... Does anyone know what's going on?

asked Oct 6, 2015 by atulpariharmca
0 votes

5 Answers

0 votes

First of all I'm assuming that SpriteKit doesn't make any difference.

  1. You need your font in .otf or .ttf copied to your project. For example in Supporting Files.
  2. You need to edit .plist file. Add "Fonts provided by application" key into your plist and in Item 0 copy the exact filename of the font you copied to your Supporting files WITH extension. For example: "JosefinSansStd-Light_0.otf"
  3. Make sure that the font you imported to your app is being packed into app itself. Do that by selecting your Target, then Build Phases, then Copy Bundle Resources. If you don't see your font in there, drag it from Supporting Files.

Finally, you would like to list all your fonts when the app starts just to see useable name for your font. You will do that with this little piece of code:

NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
    NSString *fontFamily = [fontFamilies objectAtIndex:i];
    NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
    NSLog (@"%@: %@", fontFamily, fontNames);

Search for your font in printed results, for example, I would search for "Josefin" and I would see that actual font name is "JosefinSansStd-Light". After that you only need to use that font by:

UIFont *customFont = [UIFont fontWithName:@"JosefinSansStd-Light" size:20];

In iOS8 you add your fonts directly to the project and they are visible in the interface builder. Modify your code to account for this but programmatically setting font for iOS7 and selecting it in xCode6 interface builder. PS. Interface builder in xCode6 gives you the correct font name that you can copy-paste into the code below.

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

        UIFont *customFont = [UIFont fontWithName:@"OpenSans-Light" size:32];
        self.registerLabel.font = customFont;  

Hope this helps, cheers.

answered Oct 6, 2015 by mcasudhir
0 votes

No problem to use custom fonts, I did it. Follow these steps:

  1. Register the font in your info.plist adding the "Fonts provided by application" entry. Put the font file name, not the font name. Like font.ttf (I think you already did that)
  2. Go to your project on the project navigator, click on the project name and go to "build phases" then look down in "Copy Bundle Resources" go to the bottom, there are a lot of files and search for the "+" sign. A popup window will open with your project files. Search for your font file and add it to the bundle.
  3. Finally check the font name. When you use it in your code, you have to use the font name, not the file name. To know the font name, double click on the font file in Finder and a window will open showing the font. Check that window title, that's the name of the font that you have to use.

And advice, add that name as a macro in your ..Prefix.pch file. It will be easier to use it in your project if you have to use the font multiple times.

The problem that I think you had is that you properly registered the font but it was not copied to the app bundle. So when the app executed it couldn't find the font. That's why step 2 is important. I spent a lot of time trying to figure that out the first time :)

answered Oct 6, 2015 by mtabakade
0 votes

To achieve this in swift

If you would like to list all your fonts when the app starts just to see useable name for your font. You can do that with this little piece of code:

    let fontFamilies:NSArray = UIFont.familyNames()

    for var i = 0; i < fontFamilies.count; ++i
        let fontFamily: NSString = fontFamilies.objectAtIndex(i) as! NSString;
        let fontNames: NSArray = UIFont.fontNamesForFamilyName(fontFamilies.objectAtIndex(i) as! String)
        print("\(fontFamily)  \(fontNames)");
answered Oct 6, 2015 by sachin valanju
0 votes

If you're in Xcode 6 it's as easy as it gets. Just add a font folder into your project, add your font(s) into the folder, and you're good to go.

enter image description here

answered Oct 6, 2015 by nimisha.jagtap
0 votes

Maybe a quick fix - this did it for me when the font has already worked, but a new e.g. Label didn't show the desired font:

clean, deep clean and empty derived data folder.

answered Oct 6, 2015 by abhi