Using Custom Fonts

Discussion in 'iOS Programming' started by orangeiris, Sep 24, 2008.

  1. orangeiris macrumors newbie

    Sep 11, 2008
    Is it true that you can't use custom fonts in apps? By custom, I mean any TTF that's not already available on the system?
  2. robbieduncan Moderator emeritus


    Jul 24, 2002
  3. BNicholson macrumors newbie

    Apr 7, 2008
    This is incorrect I am currently loading custom .ttf's and using them to draw text onto the screen with several of my applications. Look into CGFontCreateWithDataProvider and how to draw text in a - (void)drawRect:(CGRect)rect method.

    Hope this helps!

  4. robbieduncan Moderator emeritus


    Jul 24, 2002
    Well you learn something every day. Last time this was asked no-one had a solution. Of course you cannot distribute the fonts with your application without an appropriate license...
  5. Luke Redpath macrumors 6502a

    Nov 9, 2007
    Colchester, UK
    This is an important point that I think needs re-iterating. If you're using a custom font that you've purchased you need to check the license under which you purchased it as it will almost certainly preclude you from distributing the font itself.
  6. orangeiris thread starter macrumors newbie

    Sep 11, 2008
    Thanks Brent! I'll look into this. And, of course, make sure I'm using a font with an appropriate license.
  7. BNicholson macrumors newbie

    Apr 7, 2008
    Heya orangeiris,
    Since the NDA is lifted I would like to give you a little more of my code so you don't have to go through all of the pains I did trying to do this task.

    1. I am using this code in a UILabel class override, which isn't really necessary at all you could get the same functionality out of a UIView override with a few more member variables.

    2. Here is how I load the font:

    // Get the path to our custom font and create a data provider.
    NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"FontToLoad" ofType:@"ttf"]; 
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
    // Create the font with the data provider, then release the data provider.
    CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider);
    // Create the matrix to flip any text drawn to a readable orientation.
    CGAffineTransform textTransform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);

    3. Now after that is all completed you have loaded your first custom font! But this is where the task gets a little bit overly complicated, we need to draw with the font. All of the following code is placed in a
    - (void)drawRect:(CGRect)rect

    // Get the context.
    CGContextRef context = UIGraphicsGetCurrentContext();
    // Set the customFont to be the font used to draw.
    CGContextSetFont(context, customFont);
    // Set how the context draws the font, what color, how big.
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetFillColorWithColor(context, self.textColor.CGColor);
    CGContextSetFontSize(context, self.font.pointSize);
    // Create an array of Glyph's the size of text that will be drawn.
    CGGlyph textToPrint[[self.text length]];
    // Loop through the entire length of the text.
    for (int i = 0; i < textLength; ++i) {
         // Store each letter in a Glyph and subtract the MagicNumber to get appropriate value.
         textToPrint[i] = [self.text characterAtIndex:i] - 23;
    *****Please take note that I am subtracting 23 from the character's numeric value to get the correct ascii value. This may not be needed for your font or it maybe a different number. For the most part 23 seems to be the magic number for me, I have tried loading many fonts and the majority of them are correct with 23 and only two out of about a total of 25 I tried needed a different number. I honestly have no idea why this is needed other then to draw the correct characters. If you find a different way to do this or a better way let me know I am interested in seeing if there is a more refined way to approach this.******

    From this point all that is needed is to actually draw the text.

    I would suggest using one of the functions that are for "Drawing Glyphs". If you can't find what these are, search for the CGContext Reference it has them in there.

    I have left out ALL of the code I have for modifying the spacing when drawing text as I felt it isn't relevant for the time being. Once you get all this working let me know and I will be more then willing to help you out getting the spacing fixed if you have any issues. Best of luck!

  8. helba macrumors newbie

    Oct 2, 2008
    errr i'm a noob, how would call it?
    Like I have a function, and I call the function from a button.
    How would I call this in the function that the button is using?
    Like I want to say Hello in a custom font? thanks =D
  9. Warder macrumors newbie

    Oct 22, 2008
    Fixed most of my other problems, how do I center the text?
  10. khushsingh88 macrumors newbie

    Nov 13, 2008
    Total noob

    Hi im a total noob and need help on using this to load a gurmukhi ttf i have. all i want to do is type text into UITextView on interface builder and let the coding display using the ttf. I really hope this is possible and someone could be nice enough to post some code. I know its a hell of a lot to ask for. but you'd be helping out a lot.

  11. micksabox macrumors newbie

    Jan 30, 2009

    Hey man, just wondering to the guy who got custom fonts to work:

    What kind of member variables would you add if you were to load the font in a UIView class override? Also, where could you find the official font name to call, as I have been loading the font but it seems to create a NIL CGFontRef because the font name that I pass apparently doesnt work. For example, my font name is called Zit Graffiti but the filename is ZITGRAFF.TFF. When I search for it in Spotlight it gives me zit_graffiti. Any of these could be the name I should use, but which one is it?

    I've been literally trying to get this to work for DAYS and would really appreciate any help frmo anybody who has gotten custom fonts to work in their apps. In most cases I follow the instructions exactly and I cannot get the custom font to appear.
  12. supertoto macrumors newbie

    Jul 29, 2008
  13. sciencekris macrumors newbie

    May 18, 2010
    Custom Font

    HI Friend , can you share a sample App code for adding custom Font..

  14. robbieduncan Moderator emeritus


    Jul 24, 2002
    See all that code in the post you quoted? That's it. Spoon-fed to you in tiny digestible chunks. What more can you possibly want?
  15. TinaTwilight macrumors newbie

    Oct 7, 2013
    it isn't possible to style your text with a custom font embedded via CSS, while preventing people from downloading it. You need to use images, Flash, or the HTML5 Canvas, all of which aren't very practical.

Share This Page