18

What must I do to get an UIFont object with a custom font? I remember there was also something going on in the Info.plist file.

What font file formats are supported?

openfrog
  • 40,201
  • 65
  • 225
  • 373
  • 4
    possible duplicate of [Can I embed a custom font in an iPhone application?](http://stackoverflow.com/questions/360751/can-i-embed-a-custom-font-in-an-iphone-application) – Brad Larson Oct 01 '10 at 14:14
  • Helpful tutorial: http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/ – Suragch Feb 25 '15 at 05:59

2 Answers2

54

To add a custom font to your application, you can add them to the XCode project. Then, modify the application-info.plist file. Add the key Fonts provided by application to a new row. Add one item for each font you have added.

It supports both TTF and OpenType fonts. One caveat is that it loads and parses all fonts in the startup of your app, so it will slow down the initial load time.

I believe this is only available in iOS 3.2 or later.

Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
jschmidt
  • 2,898
  • 3
  • 24
  • 27
2

Programming way (Swift)

Add the fonts to your project at the resource folder
And add this function:

func loadFont(filePath: String) {

    let fontData = NSData(contentsOfFile: filePath)!

    let dataProvider = CGDataProviderCreateWithCFData(fontData)
    let cgFont = CGFontCreateWithDataProvider(dataProvider)!

    var error: Unmanaged<CFError>?
    if !CTFontManagerRegisterGraphicsFont(cgFont, &error) {
        let errorDescription: CFStringRef = CFErrorCopyDescription(error!.takeUnretainedValue())
        print("Unable to load font: %@", errorDescription)
    }
}

Use example:

if let fontPath = NSBundle.mainBundle().pathForResource("MyCustomFont", ofType: "ttf") {
    loadFont(fontPath)
}

let myFont = UIFont(name: "MyCustomFont", size: CGFloat(18))
Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
Daniel Krom
  • 9,751
  • 3
  • 43
  • 44