20 December 2015

Your app can be more attractive and feel better to users by using custom fonts. Getting them installed and using them is a little tricky.

Font files have to be added in Xcode as a member of your build target. They should also be included in the Copy Bundle Resources portion of the Build Phases in your project.

  1. The first step is having them added to your target’s Info.plist under the Fonts provided by application key. Each member of the Array is the full filename of the font that has been added to your target.

  2. The second step is knowing the name to use in your code. The font names can be listed using the following Swift code.

// List all of the installed fonts in iOS.
for familyName in UIFont.familyNames() {
    for fontName in UIFont.fontNamesForFamilyName(familyName) {
	    print("family \(familyName), font \(fontName)")
	}
}
// List all of the installed fonts in OS X.
for familyName in NSFontManager.sharedFontManager().availableFontFamilies {
    for fontName in NSFontManager.sharedFontManager().availableMembersOfFontFamily(familyName)! {
        print("family \(familyName), font \(fontName)")
    }
}

Here’s an example of code that changes the navigation bar’s title font text in iOS.

navigationController?.navigationBar.titleTextAttributes = [
	NSFontAttributeName: UIFont(name: "MY_FONT_NAME", size: 32)!,
	NSForegroundColorAttributeName: UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1)
]


blog comments powered by Disqus