UIFont/NSFont extension to handle custom fonts in Swift (iOS/macOS)

December 19, 2017 - 2 minute read -
ios swift macos

I’ve just finished watching interesting presentation “To be! Or not? Optionals in practice” by Rob Napier And I’d like to share one of Rob’s good ideas how to handle custom fonts with UIFont extension + NSFont extension for macOS

Here is a code snippet for UIFont:

import UIKit

extension UIFont {
	private static func customFont(name: String, size: CGFloat) -> UIFont {
		let font = UIFont(name: name, size: size)
		assert(font != nil, "Can't load font: \(name)")
		return font ?? UIFont.systemFont(ofSize: size)
	}
	
	static func mainFont(ofSize size: CGFloat) -> UIFont {
		return customFont(name: "MyLovely-CustomFont", size: size)
	}
}

and NSFont version:

import Cocoa

extension NSFont {
	private static func customFont(name: String, size: CGFloat) -> NSFont {
		let font = NSFont(name: name, size: size)
		assert(font != nil, "Can't load font: \(name)")
		return font ?? NSFont.systemFont(ofSize: size)
	}
	
	static func mainFont(ofSize size: CGFloat) -> NSFont {
		return customFont(name: "MyLovely-CustomFont", size: size)
	}

}