1

I am trying to encapsulate and organise my Xcode project per features. To achieve this, I am creating a Framework project embedded in the main project. The framework contains all the app appearance (with color definition and custom fonts).

My project structure is something similar to this:

MainProject
    |_ Appearance.xcodeproj
    |       |_ ... 
    |       |_ Controller.swift
    |       |_ Resources
    |             |_ Font1.ttf
    |             |_ Font2.ttf
    |       |_ Products
    |             |_ Appearance.framework
    |_ ...

The idea is to compile Appearance.framework only once and use embedded custom fonts from the main project. Although, the headers are exposed and there is no compilation errors, I am getting runtime errors.

The controller in the framework uses UIFont(name: "Font1", size: 9)! But, the return is nil

I read about adding the fonts fullpath on main project Info.plist. Nevertheless, the framework is local and not managed by cocoapods. So the output is derived data.

Is this the proper way to embedded this kind of framework? Can anyone provide a solution for this purpose? I don't have any problem on changing my approach.

UPDATE 1:

Also, I noticed the framework is generated in XCode Derived Data folder. There is any chance, the framework can be generated on the local folder, so it can be tracked on GIT?

3
  • Did you register your font in Info.plist file? Commented Oct 15, 2018 at 7:20
  • Yes. I registered the font in Framework's plist. I also did on MainProject one. Commented Oct 15, 2018 at 7:21
  • Have you experimented with the build phases making a script to copy them to the main target folder ? Also did you register the fonts on Both the Main target and the framework ? - Not sure if that last one works Commented Nov 2, 2018 at 16:24

1 Answer 1

10

This is how you register the fonts in memory:

extension UIFont {
    private static func registerFont(withName name: String, fileExtension: String) {
        let frameworkBundle = Bundle(for: AnyClassFromFramework.self)
        let pathForResourceString = frameworkBundle.path(forResource: name, ofType: fileExtension)
        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("Error registering font")
        }
    }

    public static func loadFonts() {
        registerFont(withName: "Font1", fileExtension: "ttf")
        registerFont(withName: "Font2", fileExtension: "ttf")
    }
}

Call UIFont.loadFonts() in AppDelegate.swift.

You should be able afterward to create your UIFont(name: "Font1", size: 17).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.