iOS应用程序的非捆绑式外部字体

时间:2018-11-11 12:30:24

标签: ios swift fonts external

如何在我的iOS应用程序中使用非捆绑自定义字体?我想通过iCloud或从互联网向应用程序添加字体,然后在内部使用它。

1 个答案:

答案 0 :(得分:2)

是的,您可以通过

guard let fontData = NSData(contentsOfFile: pathForResourceString) else {
    fatalError("[UIFont] Could not get data of font")
}

guard let dataProvider = CGDataProvider(data: fontData) else {
    fatalError("[UIFont] Could not get dataprovider of font")
}

guard let fontRef = CGFont(dataProvider) else {
    fatalError("[UIFont] Could not create font")
}

var errorRef: Unmanaged<CFError>?
if CTFontManagerRegisterGraphicsFont(fontRef, &errorRef) == false {
    fatalError("[UIFont] Could not register font")
}

https://developer.apple.com/documentation/coretext/1499499-ctfontmanagerregistergraphicsfon

您只需要引用文件或数据引用。该代码应该是不言自明的:

{{1}}

然后,您可以通过字体名称访问字体,就像包中的字体一样。