How do I get the font name from an otf or ttf file?

前端 未结 15 1677
-上瘾入骨i
-上瘾入骨i 2020-11-28 03:44

I have used a custom font in my previous app.

The file name was \"ProximaNova-Regular.otf\" and to load the font I just used...

[UIFont fontWithName:         


        
15条回答
  •  清歌不尽
    2020-11-28 03:50

    If you want to find the font name for a given font file programmatically:

    import Foundation
    
    func loadFontName(for file: URL) throws -> String {
        let data = try Data(contentsOf: file)
    
        guard let provider = CGDataProvider(data: data as CFData) else {
            throw Error("Could not create data provider")
        }
    
        guard let font = CGFont(provider) else {
            throw Error("Could not load font")
        }
    
        guard let name = font.postScriptName else {
            throw Error("Could not get font name from font file")
        }
    
        return name as String
    }
    

    Replace with your own throwable Error objects as required.

提交回复
热议问题