Download and install custom font in iOS

I searched here for the answer to this question, but I did not find any messages that can be used to get the answer. I just found answers about preinstalling the font in a plist application that will not work for me.

What I'm trying to do is download the font from the URL, save the file locally in the application, and then install or use this font in the controls.

I want to install a font on a tablet. I can add it to CTFontManager without any problems using the code below.

private func InstallFont(dest:String)
{
    let fontData = NSData(contentsOfFile: dest)!

    let dataProvider = CGDataProviderCreateWithCFData(fontData)
    let cgFont = CGFontCreateWithDataProvider(dataProvider)!

    var error: Unmanaged<CFError>?
    if !CTFontManagerRegisterGraphicsFont(cgFont, &error)
    {
        print("Error loading Font!")
    }
}

, , , , - , , , , , .

self.font = UIFont(name: "Font Name", size: 20.0)

, , .

for x in UIFont.familyNames()
{
    print(x)

    for z in UIFont.fontNamesForFamilyName(x)
    {
        print("== \(z)")
    }
}

, ? , ?

+4
2

. github. .

if !CTFontManagerRegisterGraphicsFont(cgFont, &error)
{
    print("Error loading Font!")
}
else
{
    let fontName = CGFontCopyPostScriptName(cgFont)
    uiFont = UIFont(name: String(fontName) , size: 30)
}

, .

Github

enter image description here

+3

, ( , ), Postscript UIFont:

var uiFont : UIFont?
let fontData = NSData(contentsOfFile: dest)!

let dataProvider = CGDataProviderCreateWithCFData(fontData)
let cgFont = CGFontCreateWithDataProvider(dataProvider)!

var error: Unmanaged<CFError>?
if !CTFontManagerRegisterGraphicsFont(cgFont, &error)
{
    print("Error loading Font!")
} else {
    let fontName = CGFontCopyPostScriptName(fontRef)
    uiFont = UIFont(name: fontName, size: 30)
}
+1

Source: https://habr.com/ru/post/1618101/


All Articles