RSS
 

Custom fonts on iPad and iPhone

Posted on Wednesday, August 18, 2010
18 Aug

Just incase you didn’t realise, with iOS 3.2 (iPad) and above you can load in custom fonts and use them with a standard UIFont object. There are a few catches… The font must be in the following format: –

  • OpenType Format (OTF)
  • TrueType Format (TTF)

Once you’ve dragged your chosen font file into an Xcode project, the next step is to add a line into the application’s Info.plist file. Add a new key UIAppFonts and make it an array. Expand the array and add a new string for each font, making the string the file’s full name including an extension.

Xcode Screenshot

You’re all set up now to use the font. That would be great if you knew which font it was! Here is a great little snippet for looping through all the fonts loaded into the system. Scan through the list and find your font.

// Get all the fonts on the system
NSArray *familyNames = [UIFont familyNames];
for( NSString *familyName in familyNames ){
	printf( "Family: %s \n", [familyName UTF8String] );
	NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
	for( NSString *fontName in fontNames ){
		printf( "\tFont: %s \n", [fontName UTF8String] );
	}
}

To use your font now, just use the standard UIFont constructor…

self.titleLabel.font = [UIFont fontWithName:@"Inkpen Medium" size:31.0];

Some points to note: –

  • You can also use the font inside UIWebViews.
  • Interface Builder for XCode 3.2 has a bug that won’t let you choose the font. You have to do it in code.
  • Loading in too many fonts will slow your loading time down and will hurt your users’ eyes.
 

Tags: , , , , ,

Leave a Reply

 

 
  1. Ankur Oberoi

    November 18, 2010 at 5:39 am

    I am finding an odd issue with this feature involving the order in which those strings are added to the UIAppFonts array. I am using two .otf files that represent two different font styles in the same family (Knockout-27.otf and Knockout-30.otf) as well as MyriadPro.otf. Using your code snippet I can see that all 3 of these custom fonts are available for the UIFont class. However, when trying to set these fonts, the two that are of the same family will not show up distinctly. What I mean is that I use them in two differently places in the App so I need both, but if I have Knockout-27.otf first in the array, then all labels that should have had fonts from the Knockout family use that style. If I put Knockout-30.otf in the file first, vice-versa. has anyone else experienced this problem? Where else can I look for a solution. It happens on device and in the simulator, in iOS4.1 and iOS4.2.

     
    • Ankur Oberoi

      November 18, 2010 at 6:18 am

      UPDATE: found a discussion on Apple’s dev forums that confirms that others have faced this issue. Seems to be a bug with no known workaround right now. I even tried converting my .otf files to .ttf and seeing if they were handled any differently, but that did not fix it either.

       
      • Richard Warrender

        November 18, 2010 at 8:45 am

        @Ankur, I had a similar problem actually and it seems to be a problem with the fonts. You can fix them using FontForge and changing the family name. See this post for details – https://devforums.apple.com/message/324012 Credit to Jeff Kelley for discovering a fix for this one.

         
  2. Michael Damstra

    July 17, 2012 at 8:24 pm

    Hey,

    I want to use the capitals.dfont in xcode
    capitals is a basic mac font, which can be found in fontlibrary app.
    I am using xcode 4.3.3 and building for iphone.
    I only work with storyboard so please help me with very easy instructions

    tnx

     
  3. sohail

    September 12, 2012 at 11:02 am

    when is add custom font file value in the info.plist and run the app in simulator;my simulator get hang in attaching to app name;after that app does not run over simulator. i am using xcode 4.3.1 ios 5.1

     
    • Pallavi

      January 8, 2013 at 11:57 am

      How to add custom font in xib. plz reply….

       
  4. Mills Joshi

    July 12, 2013 at 9:29 am

    Awesome :) Really great and simple tutorial :)

     
  5. http

    July 27, 2014 at 3:24 am

    We’re a bunch of volunteers and opening a new scheme in our community.
    Your website offered us with useful information to work on. You have performed a formidable job and our whole group will be grateful to you.

     
  6. forbrukslån

    August 8, 2014 at 1:44 am

    Good way of telling, and good post to take facts regarding my presentation focus, which i am going to
    convey in institution of higher education.

     
  7. Invisible Inc Full Game Download

    August 16, 2014 at 2:11 pm

    When I originally left a comment I appear to have clicked the -Notify me
    when new comments are added- checkbox and from now on whenever
    a comment is added I get four emails with the same comment.

    Perhaps there is a way you can remove me from that service?
    Cheers!

     
  8. Http://Www.Tgsurgeryjapan.Com/

    August 19, 2014 at 6:32 am

    That is a very good tip especially to those fresh to the blogosphere.

    Brief but very accurate info… Appreciate your sharing this one.
    A must read article!

     
  9. scribd.com

    September 8, 2014 at 2:52 am

    I do not know if it’s just me or if perhaps everyone else encountering problems
    with your website. It appears as if some of the text on your content are running off the screen. Can somebody else please comment and let
    me know if this is happening to them too? This could be a problem with my web
    browser because I’ve had this happen previously.
    Kudos