Monday, 7 October 2019

List of installed fonts

I am working on an application where the user can install different custom fonts. The application presents the user a table list of fonts he can install. (3 random fonts provided for this example)

enter image description here

The fonts are downloaded when the user opens the app, and the fonts are dynamically loaded with this function. The custom fonts are not added in the info.plist.

func install_font(font_path : String) -> Bool
{
    let font_data = try! Data(contentsOf: URL(fileURLWithPath: font_path))

    if let provider = CGDataProvider.init(data: font_data as CFData)
    {
        var error: Unmanaged<CFError>?

        let font:CGFont = CGFont(provider)!

        if (!CTFontManagerRegisterGraphicsFont(font, &error))
        {
            print(error.debugDescription)
            return false
        }
        else
        {
            return true
        }
    }

    return false
}

Until now I was using this function, but this displays all fonts: installed ones and the dynamically loaded.

func show_all_fonts()
{
    UIFont.familyNames.forEach({ familyName in
        let fontNames = UIFont.fontNames(forFamilyName: familyName)
        print(familyName, fontNames)
    })
}

Is there any way to display only the installed fonts? I need a method to differentiate the installed fonts vs the fonts dynamically loaded, so I can put a checkmark on table cell for the installed ones.



from List of installed fonts

No comments:

Post a Comment