29
May
09

Custom Fonts

So we have an occasion to be interested in embedding custom fonts in an iPhone application. Specifically, this DS-Digital font. It turns out that custom fonts are not quite as easy as one would hope to work with in an iPhone application. As a matter of fact, if you look around the ‘net, you’ll find opinions ranging from “it would be really hard” to “it’s not actually possible”, with people doing things like rendering fonts into UIImages to do that kind of thing since they can’t sort it out from the documentation. Yoiks! We’re looking for something rather more transparent than that! (Although we do note that the tool linked there would be quite handy indeed for using custom fonts in actual OpenGL work, like games … as would that author’s texture creator here.)

But fear not — although rather (unduly, in fact, someone less gracious than ourselves might observe…) challenging, it is, indeed, perfectly possible to use honest-to-goodness embedded CGFonts for text rendering in your iPhone app! To skip over lightly all the pitfalls, simply download this fine, fine piece of work which sorts out all the issues with a FontManager class to load the fonts and a FontLabel class to display them, with a CGFontRef-savviness category on NSString to help out … and:

fontsuccess

Sweet! The only trivial bit of trickery left to use it exactly as provided is to make sure that your font file name matches the font contained therein. Which the DOS-friendly filename from the above link didn’t, of course. But that is trivially corrected by double-clicking it in the Finder and reading off the name it displays.

So now that you know how, let’s round up sources of free fonts for your embedding joy, shall we? We focus on free sources here not because we’re cheap but because common license terms of commercial fonts are going to not be happy in the slightest with redistribution through application embedding. So unless you’re absolutely 100% sure that the IP lawyersharks aren’t going to hunt you down and feed on your entrails for unauthorized distribution of commercial fonts that you have bought, it behooves you greatly to only use fonts with freely redistributable license terms. Which we believe that you’ll find in abundance — or at least here and there — at these sites:

Any other sources of license-unencumbered embeddable fonts you care to recommend, Dear Readers?


13 Responses to “Custom Fonts”


  1. 1 Ricardo Quesada Jun 9th, 2009 at 3:27 pm

    Nice article!

  2. 2 Benjamin Aug 11th, 2009 at 12:07 am

    Hey, thanks for writing this up! This will help me out a lot. Love your writing style and the blog was easy on my eyes. Cheers!

  3. 3 Mauricio Giraldo Aug 19th, 2009 at 9:33 pm

    Dude VERY nice link. Thanks a lot! Worked like a charm.

  4. 4 Mauricio Giraldo Aug 20th, 2009 at 2:55 am

    How would you make the text clickable? Tried with subviewing a fontlabel into a uibutton to with no success.

  5. 5 Alex Aug 20th, 2009 at 6:28 am

    Well, the FontLabel class is simply a strangely drawing UILabel, so you’ve got a couple options:

    1) Add tap handling and fake buttonish drawing to UILabel

    2) Add a FontLabel as a subview (or grouped with) a blank titled UIButton.

    Whichever is more expeditious for exactly how you want your semi-custom interface to look. I suspect if I was using custom fonts for anything other than the big “scoreboard” display this was for, I’d probably have custom button state images as well, so I’d draw them myself in my FontButton subclass of FontLabel and add tap handling/state tracking. If for some reason that was impractical, then on to 2), figuring out some view nesting arrangement that works.

  6. 6 Mauricio Giraldo Aug 20th, 2009 at 1:39 pm

    Yes. I guess 3) would be the better, more future-proof option. I will look into those classes and see how extensible they are. FWIW, this is the code I am using (just a big button with a fontlabel inside):

    [code]
    - (void)viewDidLoad {
    [super viewDidLoad];
    FontLabel *btn = [[FontLabel alloc] initWithFrame:CGRectMake(0, 0, 320,480) fontName:@"Stonehenge" pointSize:28.0];
    btn.textColor = [UIColor whiteColor];
    btn.text = @"click me";
    [btn sizeToFit];
    btn.backgroundColor = [UIColor blackColor];
    btn.opaque = YES;
    UIButton *theButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    theButton.opaque = YES;
    [theButton addTarget:self action:@selector(goPlay) forControlEvents:UIControlEventTouchUpInside];
    [theButton addSubview:btn];
    [self.view addSubview:theButton];
    [btn release];
    [theButton release];
    }
    - (void)goPlay {
    NSLog(@"hello");
    }
    [/code]

    I get an EXC_BAD_ACCESS error.

  7. 7 Mauricio Giraldo Aug 22nd, 2009 at 12:21 pm

    Ok I finally made it work with subviewing the FontLabel inside a UIButton.

  8. 8 pc.de free fonts Jan 13th, 2010 at 6:54 am

    Thanks for taking the time to write it .Nice collection of fonts.I am always looking for new fonts to spice up my designs.Huge thanks.

  9. 9 Stamps Jan 25th, 2010 at 12:20 pm

    Nice list you got there. Great job! Especially Larabie Fonts is good!

  1. 1 iPhoneKicks.com Trackback on May 30th, 2009 at 12:11 am
  2. 2 Webmaster Crap » Blog Archive » Custom Fonts at Under The Bridge Pingback on May 30th, 2009 at 1:25 pm
  3. 3 Custom Fonts at Under The Bridge - Posted In Graphics Design | Bayu Albelly - www.bhienda.com Pingback on Jun 1st, 2009 at 3:20 am
  4. 4 iSigns at Under The Bridge Pingback on Sep 6th, 2009 at 8:22 pm

Leave a Reply