Xcode: Using custom fonts inside Dynamic framework

前端 未结 9 1582
再見小時候
再見小時候 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:28

    Was able to do this with Swift 4, given that you can include resources directly in a framework bundle now:

    Typography.swift (in my framework)

    import Foundation
    
    private class MyDummyClass {}
    
    func loadFontWith(name: String) {
      let frameworkBundle = Bundle(for: MyDummyClass.self)
      let pathForResourceString = frameworkBundle.path(forResource: name, ofType: "otf")
      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 func loadMyFonts() {
      loadFontWith(name: "ComicSansPro-Regular")
      loadFontWith(name: "ComicSansPro-Medium")
      loadFontWith(name: "ComicSansPro-Bold")
      loadFontWith(name: "ComicSansPro-ExtraBold")
    }
    

    I ended up requiring the loadMyFonts method to be called in the application's that use this framework's didFinishLaunchingWithOptions method in AppDelegate.swift.

提交回复
热议问题