Xcode: Using custom fonts inside Dynamic framework

前端 未结 9 1549
再見小時候
再見小時候 2020-12-04 17:52

I added custom font to a framework. I followed all the steps, but it doesn\'t work.

I am able to set the font in Interface Builder, but when I build the project it d

9条回答
  •  [愿得一人]
    2020-12-04 18:21

    I made a mix of different answers in Swift 4.2 so props to the guys who came up with it!

    import UIKit
    import Foundation
    
    extension UIFont {
    
        private class MyDummyClass {}
    
        static func loadFontWith(name: String) {
            let frameworkBundle = Bundle(for: MyDummyClass.self)
            let pathForResourceString = frameworkBundle.path(forResource: name, ofType: "ttf")
            let fontData = NSData(contentsOfFile: pathForResourceString!)
            let dataProvider = CGDataProvider(data: fontData!)
            let fontRef = CGFont(dataProvider!)
            var errorRef: Unmanaged? = nil
    
            if (CTFontManagerRegisterGraphicsFont(fontRef!, &errorRef) == false) {
                NSLog("Failed to register font - register graphics font failed - this font may have already been registered in the main bundle.")
            }
        }
    
        public static let loadMyFonts: () = {
            loadFontWith(name: "Exo-Black")
            loadFontWith(name: "Exo-Bold")
            loadFontWith(name: "Exo-Regular")    
        }()
    }
    

    And then call in Appdelegate

    UIFont.loadMyFonts
    

提交回复
热议问题