Can I embed a custom font in a bundle and access it from an ios framework?

后端 未结 7 1294
北荒
北荒 2020-12-13 02:35

I\'m creating an ios framework with its bundle for packaging ressources (nib, images, fonts) and I\'m trying to embed a custom font

7条回答
  •  -上瘾入骨i
    2020-12-13 03:11

    Swift 3 version of @Ali-ABBAS's answer, also updated to up-wrap options instead of force unwrapping.

    fileprivate func loadCustomFont(name:String) -> Bool{
    
        guard let fontPath = frameworkBundle.path(forResource: name, ofType: "ttf") else {
            return false
        }
    
        guard let inData = NSData(contentsOfFile:fontPath) else {
            return false
        }
    
    
        guard let provider = CGDataProvider(data: inData) else {
            return false
        }
    
        let font = CGFont(provider)
        var error: Unmanaged?
        CTFontManagerRegisterGraphicsFont(font, &error)
        guard error == nil else {
            print(error) //Or logged it
            return false
        }
    
        return true
    }
    

提交回复
热议问题