Skip to content

Instantly share code, notes, and snippets.

@kerimdeveci
Forked from eoghain/UIFontExtensions.swift
Created January 12, 2020 09:42
Show Gist options
  • Select an option

  • Save kerimdeveci/22c5bcdec4a3dee612d372ffe0375662 to your computer and use it in GitHub Desktop.

Select an option

Save kerimdeveci/22c5bcdec4a3dee612d372ffe0375662 to your computer and use it in GitHub Desktop.

Revisions

  1. @eoghain eoghain revised this gist Dec 20, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UIFontExtensions.swift
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ import UIKit
    public extension UIFont {

    class func loadAllFonts(bundleIdentifierString: String) {
    registerFontWithFilenameString(filenameString: "tt-icon-font.ttf", bundleIdentifierString: bundleIdentifierString)
    registerFontWithFilenameString(filenameString: "icon-font.ttf", bundleIdentifierString: bundleIdentifierString)
    // Add more font files here as required
    }

  2. @eoghain eoghain created this gist Dec 20, 2016.
    26 changes: 26 additions & 0 deletions UIFontExtensions.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import UIKit

    public extension UIFont {

    class func loadAllFonts(bundleIdentifierString: String) {
    registerFontWithFilenameString(filenameString: "tt-icon-font.ttf", bundleIdentifierString: bundleIdentifierString)
    // Add more font files here as required
    }

    static func registerFontWithFilenameString(filenameString: String, bundleIdentifierString: String) {
    if let frameworkBundle = Bundle(identifier: bundleIdentifierString) {
    let pathForResourceString = frameworkBundle.path(forResource: filenameString, ofType: nil)
    let fontData = NSData(contentsOfFile: pathForResourceString!)
    let dataProvider = CGDataProvider(data: fontData!)
    let fontRef = CGFont(dataProvider!)
    var errorRef: Unmanaged<CFError>? = nil

    if (CTFontManagerRegisterGraphicsFont(fontRef, &errorRef) == false) {
    print("Failed to register font - register graphics font failed - this font may have already been registered in the main bundle.")
    }
    }
    else {
    print("Failed to register font - bundle identifier invalid.")
    }
    }
    }