Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awesome.bundle not being loaded in Mac app using cocoapods #38

Open
aaronrogers opened this issue Feb 14, 2019 · 1 comment
Open

Awesome.bundle not being loaded in Mac app using cocoapods #38

aaronrogers opened this issue Feb 14, 2019 · 1 comment

Comments

@aaronrogers
Copy link

In iOS everything works great with using cocoapods, but when using Awesome in a mac app I get the exception

Font FontAwesome5FreeRegular not loaded properly. Did you forget to call AwesomePro.loadFonts(from:)?

Is there a step that I'm missing?

In Fonts.swift the following code returns nil.

fontBundle.url(forResource: type.file, withExtension: "ttf", subdirectory: isCocoapods ? "Awesome.bundle" : nil)
@aaronrogers
Copy link
Author

Oddly enough, it looks like with macOS there's yet another Awesome.bundle in fontBundle. Changing the load code to the following appears to work on iOS and macOS.

    static func load(type: AwesomeFont, from bundle: Bundle? = nil) {
        guard !Font.fontNames(forFamilyName: type.description).contains(type.memberName) else {
            return
        }

        let fontBundle: Bundle!
        if bundle == nil {
            fontBundle = Bundle(for: Fonts.self)
        } else {
            fontBundle = bundle
        }

        var awesomeBundle = fontBundle!
        while true {
            guard
                let url = awesomeBundle.url(forResource: "Awesome", withExtension: "bundle"),
                let subBundle = Bundle(url: url)
                else {
                    break
            }
            awesomeBundle = subBundle
        }

        let fontURL = awesomeBundle.url(forResource: type.file, withExtension: "ttf")

        guard let url = fontURL else {
            return
        }

        let data = try! Data(contentsOf: url as URL)
        let provider = CGDataProvider(data: data as CFData)
        let font = CGFont(provider!)

        var error: Unmanaged<CFError>?

        if CTFontManagerRegisterGraphicsFont(font!, &error) == false {
            let errorDescription: CFString = CFErrorCopyDescription(error!.takeUnretainedValue())
            let nsError = error!.takeUnretainedValue() as AnyObject as! NSError
            NSException(name: NSExceptionName.internalInconsistencyException, reason: errorDescription as String, userInfo: [NSUnderlyingErrorKey: nsError]).raise()
        }
        
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant