Custom font in iPad application


#1

I’ve got a custom font I need to use in my iPad app. I’m including it as part of my bundle resources, and telling the iOS about it through the UIAppFonts plist entry.

When I iterate over the list of available fonts returned by Font::findAllTypefaceNames(), I see the font name. So far, so good.

Unfortunately, when it comes time to use the font to draw, the call to CGFontCreateWithFontName ((CFStringRef) fontName) in MacTypeface is returning 0.

The only issue I can think of is that my font name isn’t the same as my typeface name, thus I really want to call CGFontCreateWithFontName() using “myFont Regular” or something like that.

Is there a clever way to figure out what the needed font name is, given the font?


#2

Poking around further, replacing this line:

with

UIFont *theFont = [UIFont fontWithName:fontName size:15]; fontRef = CGFontCreateWithFontName ((CFStringRef) theFont.fontName);

allows me to use the custom font as expected.
Why would CGFontCreateWithFontName fail, and UIFont::fontWithName succeed on the same string?


#3

Well, what are the two strings? Would be interesting to see what the new name is that the UIFont class converts it to.


#4

OK, I found the difference.

If I pass in “MyFont” as the font name,
UIFont:fontWithName returns a UIFont object with the fontName “MyFont-Regular”

if I pass in “MyFont” as the font name to CGFontCreateWithFontName, the function will fail to return a valid fontRef.

This is a little tricky, since the font wasn’t installed on the system, so there’s no way to programmatically look at its name
until it is. Apple’s FontBook opens the font as “MyFont”, until its installed, at which point it shows “MyFont-Regular” at the
bottom of the window.

The gist is that UIFont seems a bit better at loading from a string. And I should add, this problem is iOS specific. The current juce code worked fine for a custom font embedded in my bundle under OSX.