Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom fonts in Xcode playground

I am writing the interface layout in code. As it is annoying to reload application every time I am testing font size or view layout pixel by pixel, i started doing it in playground. That does help a lot. But I do miss my custom fonts there.

Is there a way to add custom fonts to your playground ?

like image 641
Katafalkas Avatar asked Dec 14 '14 18:12

Katafalkas


People also ask

How do I add custom fonts to Xcode?

To add a font file to your Xcode project, select File > Add Files to “Your Project Name” from the menu bar, or drag the file from Finder and drop it into your Xcode project. You can add True Type Font (. ttf) and Open Type Font (. otf) files.


1 Answers

First, add the .ttf file to the resources of your playground. Then you can load the font like this:

let cfURL = NSBundle.mainBundle().URLForResource("Proxima Nova Semibold", withExtension: "ttf") as! CFURL

CTFontManagerRegisterFontsForURL(cfURL, CTFontManagerScope.Process, nil)

let font = UIFont(name: "ProximaNova-Semibold", size:  14.0)

The filename of the .ttf file does not usually match up with the actual desciptor name of the font, which you need for the UIFont name. To find that, open up the .ttf file in Font Book on your mac, look at its details, and look for the PostScript name. That's the name to look for in UIFont(name:...)

Alternatively you can look for your installed font after registering the URL with the following code:

var fontNames: [[AnyObject]] = []
for name in UIFont.familyNames() {
    println(name)
    if let nameString = name as? String
    {
        fontNames.append(UIFont.fontNamesForFamilyName(nameString))
    }
}
fontNames
like image 187
Chris Garrett Avatar answered Jan 01 '23 19:01

Chris Garrett