Skip to content

Instantly share code, notes, and snippets.

@JaviLorbada
Created August 31, 2015 19:09
Show Gist options
  • Select an option

  • Save JaviLorbada/4c3c07da7d9294fd3d71 to your computer and use it in GitHub Desktop.

Select an option

Save JaviLorbada/4c3c07da7d9294fd3d71 to your computer and use it in GitHub Desktop.
Load custom fonts within Swift playgrounds
//: Playground - noun: a place where people can play
import UIKit
let fontURL = NSBundle.mainBundle().URLForResource("PillGothic300mg-bold", withExtension: "ttf")
CTFontManagerRegisterFontsForURL(fontURL!, CTFontManagerScope.Process, nil)
var pillGothicFontBold = UIFont(name: "PillGothic300mg-bold", size: 30)
var attrs = [NSFontAttributeName : pillGothicFontBold!,
NSForegroundColorAttributeName : UIColor.whiteColor(),
NSBaselineOffsetAttributeName : 0.0]
let nameAttrSring = NSAttributedString(string: "Pill Gothic font 300mg Bold size 30", attributes: attrs)
@soffes
Copy link
Copy Markdown

soffes commented Mar 28, 2018

In Swift 4.1:

let fontURL = Bundle.main.url(forResource: "PillGothic300mg-bold", withExtension: "ttf")
CTFontManagerRegisterFontsForURL(fontURL! as CFURL, CTFontManagerScope.process, nil)
let font = UIFont(name: "PillGothic300mg-bold", size: 30)!

@sonle90
Copy link
Copy Markdown

sonle90 commented Aug 24, 2019

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment