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 ?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With