Download font .ttf file from web and store on iPhone

后端 未结 5 595
余生分开走
余生分开走 2020-12-18 11:39

Is it possible to download .ttf file from web and store it on iPhone. Then use that for for labels and all other stuff ? Because my client want to control fonts from databas

5条回答
  •  旧巷少年郎
    2020-12-18 12:07

    It is possible. I created an example swift project in github. You have to just add the few line below.

    var uiFont : UIFont?
    let fontData = data
    
    let dataProvider = CGDataProviderCreateWithCFData(fontData)
    let cgFont = CGFontCreateWithDataProvider(dataProvider)
    
    var error: Unmanaged?
    if !CTFontManagerRegisterGraphicsFont(cgFont, &error)
    {
       print("Error loading Font!")
    } else {
       let fontName = CGFontCopyPostScriptName(cgFont)
       uiFont = UIFont(name: String(fontName) , size: 30)
    }
    

    Github project link

提交回复
热议问题