Custom Fonts

Embedding Fonts

If you don’t want to use any of the system fonts provided by Apple, you can embed custom fonts in your applications. TrueType and OpenType fonts are supported, but I’ve never had much luck with OpenType, so you should stick to TrueType if you can.

  1. Add the TTF resource to your Xcode project, much like you would add an image resource.
  2. Add the file name to the Information Property List (Project-Info.plist), under the Fonts provided by application section.

  1. Load the font into a UIFont using its PostScript name.
  2. Use the UIFont wherever you would write text, like in a UILabel, UITextField, or UITextView.

To get the font name, you can use the Font Book application provided with your Mac. Just load the font in the program and read the PostScript name field in the Info window.

Alternatively, you can print out all the available font families and names to your app using the following UIFont class methods.

Font Attributes

There is no way to set a custom font in the Storyboard, it must be done in code. This unfortunately makes it hard to align and resize text areas, but there are some methods available to help.

sizeToFit will change the bounds of the UIView to snuggly fit around its contents.

NSString‘s sizeWithFont: will return the width and height of the string using that specific font.

The sizeWithFont:forWidth:lineBreakMode: version can also be used for text on multiple lines, and the sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: version can be used for text with variable font size. These will accurately calculate the width for UILabels with the same properties, but for some reason, the resulting CGSize only includes the first line of text.


Comments are closed.