30

It looks like there a few working solutions for using custom true type fonts (ttf) on the iPhone, most notably Can I embed a custom font in an iPhone application? However, this method does not support open type fonts (otf).

Has anyone come across a way to make use of otf typefaces?

Community
  • 1
  • 1
BP.
  • 351
  • 1
  • 4
  • 7

7 Answers7

69

For my own project I wanted to use the opentype font and I basically used this method:

Add your custom font file into your project using XCode as a resource Add a key to your info.plist file (Fonts provided by application) For each font you have, enter the full name of your font file (including the extension) save info.plist then use this for your labels (ex:)

UIFont* font = [UIFont fontWithName:@"Harrowprint" size:20];

In my case @"Harrowprint" was the filename without the extension and not the font name provided by Finder.

My font was an otf and it worked...so I guess it's supported...or I was lucky. I'll test another font..and keep you posted..

references:

Community
  • 1
  • 1
Rodo
  • 1,578
  • 1
  • 14
  • 11
  • 1
    Thank u dude, your solution is really simple and works good. Thank you a lot. – Rostyslav Druzhchenko Jun 05 '12 at 15:12
  • 7
    This should really be the accepted answer. The accepted answer is more of an unnecessary work-around. – Dermot Jan 14 '13 at 01:05
  • [This](http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/) is really a good source of information. In difference to what is outlined above, you cannot assume that the font's filename will equal the actual font name (which is what you need for the UIFont fontwithName call). – zero0cool Aug 25 '14 at 11:38
22

Must you use an otf font on the device or can you convert it to ttf? It is relatively straightforward to convert an OTF font to a TTF font using a tool such as fontforge, perhaps that would be an easy solution.

Joey Hagedorn
  • 2,408
  • 19
  • 22
  • Thanks, this worked well and subsequently converted otf->ttf font was accepted by the FontLabel code linked to above. FYI to all: Attempting to install FontForge on Snow Leopard by launching the package generated an error saying cannot be installed on "this version" of the OS. However, the app was nevertheless installed in the applications folder and worked without errors. Probably a 64 bit error with the installer package. – BP. Sep 09 '09 at 15:57
  • There is some buggy thing, as not all fonts are working. Try with other fonts too to be sure. In my case i installed font successfully to my mac, still the font not worked on iOS. – Gökhan Barış Aker Jun 26 '12 at 08:13
  • 1
    here is online tool to convert OTF into TTF http://www.freefontconverter.com/index.php – Just Variable Aug 13 '13 at 16:16
  • 4
    This link http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/ is most useful as answered below, no need to convert .otf to .ttf, This link has helped me to use .otf provided by customer. – mask Feb 14 '14 at 21:22
17

I had trouble with this, too. Here is what worked for me:

  1. Copy the font files into the Resources.
  2. Check to see that the font files are in 'Build Phases'->'Copy Bundle Resources' (Xcode put them there for me when I added the files to the Resources. If, for some reason, they are not there, add them.
  3. Add the filenames to your info.plist file as describe above. Include the extensions.
  4. Open Font Book, select your font and Preview-Show Font Info and look at the 'Family', e.g., 'Foo'
  5. Put this in your code somewhere and set a breakpoint to check the array of names: NSArray *names = [UIFont fontNamesForFamilyName:@"Foo";
  6. Run your app and when it breaks, right-click on the 'names' variable in the debugger. This will show you the font names to use, e.g.: <__NSCFArray 0xe9c4da0>( Foo-100Italic, Foo-100 )
    Note that my font names were not shown correctly anywhere else, not in the filename nor in Font Book.
  7. You should be good to go with:
    myLabel.font = [UIFont fontWithName:@"Foo-100Italic" size:24];
Emil
  • 7,220
  • 17
  • 76
  • 135
Alan McKean
  • 179
  • 1
  • 3
  • In Font-Book my font's real name was the same as "PostScript name". This is the only thing that worked for me. I have used custom fonts in the past and never had a problem until now. No clue what changed, but thanks. – Dcritelli Feb 19 '13 at 16:21
  • 1
    Points 4, 5 and 6 are really useful! – Ravi Nov 05 '13 at 20:13
8

This article answers your question (it worked like a charm for me using OTF): http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

OTF is perfectly supported, no need to convert to TTF

etayluz
  • 15,920
  • 23
  • 106
  • 151
3

With regards to the first answer - the solution is correct (i.e. just convert the font over to TTF) but the software suggested is a super hassle to compile and install. Instead I just did a quick google search and used this instead: http://www.freefontconverter.com/ and that worked perfectly.

(Make sure to add the font to your info.plist file as well)

Salman
  • 494
  • 4
  • 18
  • +1 for a good reference. However, don't rely on the answers appearing in a specific order (i.e. "the first answer") -- the order changes with user settings, votes, etc. – Caleb Jan 07 '13 at 16:36
2

I researched this throughly, and this is the best method I found:

1) Convert font file to TTF using this online tool: http://www.freefontconverter.com/

2) Add the TTF files to your project

3) Make some configurations to your info.plist file:

3.1) Edit info.plist as source code (right click on file -> open as -> source code)

3.2) Add this:

 <key>UIAppFonts</key>
 <array>
  <string>font1.ttf</string>
  <string>font2.ttf</string>
  ...etc...
 </array>

3.3) Your info.plist should have this now:

info plist should look like this now

4) Use the new fonts just like the system fonts:

[UIFont fontWithName:@"font1" size:16.0];

(Notice no ".ttf" in the font name)

nurnachman
  • 4,468
  • 2
  • 37
  • 40
  • This works perfect, thanks! Worth noting that you don't have to edit the source code of the plist, you can add these under "Custom iOS Target Properties" In the Info pane of your app's target. http://jednorog.sneakyness.com/image/2Q2O1j1l2E2Z – Sneakyness Jan 29 '14 at 06:02
0

You should use the font names, not the font file names. To get the font names, run

for family in UIFont.familyNames {
    print("font family", family)
    for names in UIFont.fontNames(forFamilyName: family) {
        print("font name \(names)")
    }
}
onmyway133
  • 45,645
  • 31
  • 257
  • 263