Wednesday, June 24, 2015

iOS Development : Using Custom Fonts

Using custom fonts on iOS is very simple, but there are few catches to watch out. This post provides details on using custom fonts on iOS and what are all precaution required to take while using them.

Adding fonts to project

Before adding...

a. Check file format

Only .ttf and .otf formats are supported as of now.

b. Is font file valid ?

Double click font file on OS X, it should open popup for font installation, click install. If it gets installed properly it will open OS X default font application named "Font Book" with your font listed in it. Most of the fonts which get installed on OS X also work fine on iOS.

So what happens when font file is invalid ?

When you double click and click the install button on a popup, this dialog comes up.

And if you checked "Select all fonts" and then clicked a button "Install Checked" even after that your font will not get installed. You will not see it inside "Font Book" either. If there is only warning of the duplicate font then there is no issue, the font already exists. For duplicate font files, you can install that font and then go to "Edit >  Look for Enabled Duplicate" to get existing font name for the same file.There is one more way to check font validation programmatically, which is explained in further point.

c. License

The license can usually be found with your font download or on the site where you bought/downloaded it. Taking a minute to check it ensures you won’t get into legal trouble down the road. Some fonts are sold through originals sites only. So, any site that wants to sell it to you for cheap or free is not legit.

Adding font files...

a. Copying to project

There is no different process for adding to the project, just drag-drop files to your project or put them in some folder for maintaining project file structure. 

b. Updating .plist 

Now go to project property list file (.plist), add key "Fonts provided by application", value for this key is of Type array add font file names as array items.

c. Bundle resource

These fonts must go along with bundle resource. To check, select project file > Select appropriate target of project > Select tab "Build Phases"  > Expand "Copy Bundle Resources". Make sure fonts you added are listed here, if not add them.

What is a name of the font ?

The tricky part, most of the times font file names are not actual font name. 

ways to check font name...

1. Open font file in default font application "Font Book" available on OS X. This will provide font name along with many details about the font.

2. After adding font to your project, file names in .plist and copying them as bundle resource

Execute the following code, this will enlist all fonts.

Check your console and use name coming in console line "Actual font name : ". This is one more way to validate font file. If font name appears in this list, then font file is valid.

NOTE : This check is possible only if your font file has some name close to the actual font, otherwise you will have to add fonts one by one, copy console log before and after adding the font to some text file. Use file merge application available on mac to check the difference in two files. 

Using custom fonts

You can use the font with name method.

    UIFont *font = [UIFont fontWithName:@"CharlotteSansMediumPlain" size:13];

Providing the incorrect name and using it will either crash an application or will show default Helvetica font, so double check by adding breakpoints / console logs if the font is not nil.

No comments:

Post a Comment