102

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 https://github.com/deni2s/IBCustomFonts but I cannot get it to work with this font http://www.fontspace.com/freaky-fonts/emulogic

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?

sangony
  • 11,636
  • 4
  • 39
  • 55
José María
  • 2,835
  • 5
  • 27
  • 42
  • Are you sure you are using the correct name for the font when using it in your code? – Nick Feb 12 '14 at 19:38
  • 1
    this blog may be helpful for you http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/ – Xu Yin Feb 12 '14 at 19:38

9 Answers9

279

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)
if (SYSTEM_VERSION_LESS_THAN(@"8.0")) {
    UIFont *customFont = [UIFont fontWithName:@"OpenSans-Light" size:32];
    self.registerLabel.font = customFont;  
}
starball
  • 20,030
  • 7
  • 43
  • 238
jovanjovanovic
  • 4,158
  • 3
  • 22
  • 32
  • Thanks! I got it! The problem was I wan't adding it to Supporting Files, so it was not loading :P – José María Feb 12 '14 at 20:31
  • 1
    I had to use the plist key of `UIAppFonts` – Mike Flynn Sep 10 '14 at 17:00
  • 1
    I recreated my app, the whole project from iOS7/Xcode 5.1.1 into a iOS8/Xcode 6.0 project. The custom font was not showing until I did the 2.step. Gr8 explanation! – MB_iOSDeveloper Oct 17 '14 at 08:36
  • Thanks for the code snippet. I had a custom font that was appearing in iOS7 but not in iOS8. Turns out that the same font file, the font name under iOS7 used _ "underscore" between elements of the name, while iOS8 uses - "dash" between elements of the name. – chadbag Oct 20 '14 at 22:11
  • 1
    The same code for getting font names in Swift: `let fontFamilies = UIFont.familyNames() for familyName in fontFamilies { let fontNames = UIFont.fontNamesForFamilyName(familyName as String) println("\(familyName): \(fontNames)") }` – Alexander Bekert Feb 12 '15 at 21:00
  • Wow thanks man, the hint with the target membership just saved me a lot of pain – d3p0nit May 07 '15 at 07:55
  • Does anyone know if info.plist modification is still needed in iOS 9/Xcode 7.3? I was able to add a custom font and deploy the app to my phone without modifying the info.plist and it still worked. – Mark Moeykens May 05 '16 at 06:09
  • 1
    Some swift code for printing all font names: `for name in UIFont.familyNames() { debugPrint(UIFont.fontNamesForFamilyName(name)) }` – gimenete May 30 '16 at 10:21
  • Here's a link showing some of the common mistakes when trying to add a font. It covers the above and a few other things to look for: http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/ – xdeleon Nov 13 '16 at 03:14
  • thanks a lot, but can't find the font if i choose the control to be attributed rather than plain – Amr Angry Dec 30 '16 at 17:39
  • **Swift3** (get all fonts): `UIFont.familyNames.forEach { print("fontFamily: \($0), fonts: \(UIFont.fontNames(forFamilyName: $0))") }` – Daniel Jan 27 '17 at 07:46
  • Since it's not said here. The name to use is the "Postscript name", that can be check with "Font Book.app" on OSX, by selecting the font and then showing its infos. Then it should match the on given (in the answer case: JosefinSansStd-Light). It may be of an help if someone rename the file name of font. – Larme May 11 '17 at 21:19
24

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 :)

zago
  • 532
  • 2
  • 10
20

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:

    UIFont.familyNames.forEach {
        print("fontFamily: \($0), fonts: \(UIFont.fontNames(forFamilyName: $0))")
    }
Toye
  • 288
  • 2
  • 13
  • or with `UIFont.familyNames.forEach { print("fontFamily: \($0), fonts: \(UIFont.fontNames(forFamilyName: $0))") }` – Daniel Jan 27 '17 at 07:47
5

I have never been able to get these answers to work having tried multiple times. The only way I have ever got a custom font in it to follow the instruction in the accepted answer then create a hidden label in the story board that uses the font. The I can use the font programatically. Weird and hacky I know but it worked for me.

Stuart Clark
  • 611
  • 8
  • 13
  • 1
    you are exactly right. I looked at everything in many tutorials, including the plist, checkmark for target the project, build phases, and finally found the correct font family name, but it still wouldnt work. Then I added a hidden label as you say and finally it showed up. I'm using Xcode 7.2 and it must be a bug. – DrZ214 Dec 21 '15 at 05:28
3

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

Idris
  • 997
  • 2
  • 10
  • 27
3

If none of the methods above worked for you, AND you happen to use your custom font "programatically" only (i.e. you didn't use it in any xib) then you will need to use it at least once somewhere in one xib, anywhere.

(P.S. Stuart Clark - already answered this, so no credit for me please. However, I decided to rephrase and post it as a separate answer, because I missed his answer, it wasn't clear to me when I read it. I found out this solution by chance when I was testing the custom font in a xib, and I saw that it worked elsewhere programatically, but when I removed the font from the xib, it stopped working programatically.)

Wojtek
  • 436
  • 5
  • 7
2

Updated for Swift 3.

let fontFamilies:[String] = UIFont.familyNames

for fontFamily: String in fontFamilies {
    let fontNames: [String] = UIFont.fontNames(forFamilyName: fontFamily)
    print("\(fontFamily)  \(fontNames)");
}
0

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.

brainray
  • 12,512
  • 11
  • 67
  • 116
0
please try below code.
@end
@implementation UILabel (CustomFont)
-(void)setFontName:(NSString *)value{
CGFloat newsize = (self.font.pointSize * [UIScreen mainScreen].bounds.size.width)/320;
[self setFont:[UIFont fontWithName:self.font.fontName size:newsize]];
}
@end
@implementation UITextField (CustomFont)
-(void)setFontName:(NSString *)value{
CGFloat newsize = (self.font.pointSize * [UIScreen mainScreen].bounds.size.width)/320;
[self setFont:[UIFont fontWithName:self.font.fontName size:newsize]];
}
@end
@implementation UIButton (CustomFont)
-(void)setFontName:(NSString *)value{
CGFloat newsize = (self.titleLabel.font.pointSize * [UIScreen mainScreen].bounds.size.width)/320;
[self.titleLabel setFont:[UIFont fontWithName:self.titleLabel.font.fontName size:newsize]];
}
@end
@end

@interface UILabel (CustomFont)
-(void)setFontName:(NSString *)value;
@end
@interface UITextField (CustomFont)
-(void)setFontName:(NSString *)value;
@end
@interface UIButton (CustomFont)
-(void)setFontName:(NSString *)value;
@end