BP.
BP.

Reputation: 351

Has anyone had success using custom otf fonts on the iPhone?

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?

Upvotes: 30

Views: 27255

Answers (7)

onmyway133
onmyway133

Reputation: 48165

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)")
    }
}

Upvotes: 0

etayluz
etayluz

Reputation: 16436

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

Upvotes: 8

nurnachman
nurnachman

Reputation: 4565

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)

Upvotes: 2

Alan McKean
Alan McKean

Reputation: 179

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

Upvotes: 17

Salman
Salman

Reputation: 494

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)

Upvotes: 3

Rodo
Rodo

Reputation: 1588

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:

Upvotes: 69

Joey Hagedorn
Joey Hagedorn

Reputation: 2408

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.

Upvotes: 22

Related Questions