Download font .ttf file from web and store on iPhone

半世苍凉 提交于 2019-11-30 18:38:29

问题


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 database and don't want to just drop fonts to xcode project right away.

So in future if he wants to change font, he will add new font to database, app will recognize new font on web (thats already done with images, not a problem), download it and use as font.

Thanks.


回答1:


The fonts have to be set in the plist of your app, and that file cannot be changed during runtime, so you need to compile your project with the fonts already added to it.

You'll have to think in other way of implementing it.




回答2:


Actually it is possible to dynamically add fonts to the iOS runtime like this:

NSData *fontData = /* your font-file data */;
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
    CFStringRef errorDescription = CFErrorCopyDescription(error)
    NSLog(@"Failed to load font: %@", errorDescription);
    CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);

Source: This Blog Article of Marco Arment.




回答3:


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<CFError>?
if !CTFontManagerRegisterGraphicsFont(cgFont, &error)
{
   print("Error loading Font!")
} else {
   let fontName = CGFontCopyPostScriptName(cgFont)
   uiFont = UIFont(name: String(fontName) , size: 30)
}

Github project link




回答4:


You could use FontLabel (https://github.com/vtns/FontLabel) or smth. similar to load ttfs from the file system. I don't think that you can use downloaded fonts with a UILabel. Because you need the plist entries for each font.




回答5:


Swift 4 solution by extension:

extension UIFont {

    /**
     A convenient function to create a custom font with downloaded data.

     - Parameter data: The local data from the font file.
     - Parameter size: Desired size of the custom font.

     - Returns: A custom font from the data. `nil` if failure.

     */
    class func font(withData data: Data, size: CGFloat) -> UIFont? {

        // Convert Data to NSData for convenient conversion.
        let nsData = NSData(data: data)

        // Convert to CFData and prepare data provider.
        guard let cfData = CFDataCreate(kCFAllocatorDefault, nsData.bytes.assumingMemoryBound(to: UInt8.self), nsData.length),
            let dataProvider = CGDataProvider(data: cfData),
            let cgFont = CGFont(dataProvider) else {
            print("Failed to convert data to CGFont.")
            return nil
        }

        // Register the font and create UIFont.
        var error: Unmanaged<CFError>?
        CTFontManagerRegisterGraphicsFont(cgFont, &error)
        if let fontName = cgFont.postScriptName,
            let customFont = UIFont(name: String(fontName), size: size) {
            return customFont
        } else {
            print("Error loading Font with error: \(String(describing: error))")
            return nil
        }

    }
}

Usage:

let customFont = UIFont.font(withData: data, size: 15.0)


来源:https://stackoverflow.com/questions/16667327/download-font-ttf-file-from-web-and-store-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!