从iOS 13开始,CTFontManager
具有以下功能:
@discussion Font assets are extracted from the asset catalog and registered. This call must be made after the completion handler of either NSBundleResourceRequest beginAccessingResourcesWithCompletionHandler: or conditionallyBeginAccessingResourcesWithCompletionHandler: is called successfully.
Name the assets using Postscript names for individual faces, or family names for variable/collection fonts. The same names can be used to unregister the fonts with CTFontManagerUnregisterFontDescriptors. In iOS, fonts registered with the persistent scope are not automatically available to other processes. Other process may call CTFontManagerRequestFonts to get access to these fonts.
@param fontAssetNames
Array of font name assets in asset catalog.
...
CTFontManagerRegisterFontsWithAssetNames(_ fontAssetNames: CFArray, _ bundle: CFBundle?, _ scope: CTFontManagerScope, _ enabled: Bool, _ registrationHandler: ((CFArray, Bool) -> Bool)?)
然而,资产目录没有任何方式添加“字体资产”。
我试过的:
kanitregular.ttf
(后记名称为kanit-regular
)。kanit-regular
的数据资产。kanit-regular.ttf
并将其放入数据资产。数据资产的contents.json
现在如下所示:
{
"data" : [
{
"filename" : "Kanit-Regular.ttf",
"idiom": "universal",
"universal-type-identifier" : "public.truetype-ttf-font"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
CTFontManager
像这样:
func registerFont() {
var cfBundle: CFBundle?
if let bundle = Bundle(for: type(of: self)) {
cfBundle = CFBundleCreate(kCFAllocatorDefault, bundle.bundleURL as CFURL)
}
CTFontManagerRegisterFontsWithAssetNames(["Kanit-Regular"] as CFArray, cfBundle, .persistent, true) { (errors, done) -> Bool in
print(errors)
return done
}
}
在此之后,打印错误
:
▿ 1 element
- 0 : Error Domain=NSPOSIXErrorDomain Code=22 "Invalid argument" UserInfo={CTFontManagerErrorFontAssetNameKey=(
"Kanit-Regular"
)}
有什么办法让它起作用吗?
通过以下操作使其工作:
字体
按需资源标记来标记KANIT-Regular
数据资产(标记名称可以是您首选的名称,字体
只是示例)。字体
标记放入初始安装标记
预取资源标记部分签名和功能
中添加字体
功能,并在其中的所有框中打勾像这样:
func registerFont() {
var cfBundle: CFBundle?
var resourceRequest: NSBundleResourceRequest?
if let bundle = Bundle(for: type(of: self)) {
resourceRequest = NSBundleResourceRequest(tags: Set(arrayLiteral: "fonts"), bundle: bundle)
cfBundle = CFBundleCreate(kCFAllocatorDefault, bundle.bundleURL as CFURL)
}
resourceRequest?.beginAccessingResources() { error in
if let error = error {
print(error)
} else {
CTFontManagerRegisterFontsWithAssetNames(["Kanit-Regular"] as CFArray, cfBundle, .persistent, true) { (errors, done) -> Bool in
print(errors)
return done
}
}
}
}
结果
当作用域作为.persistent
或.user
传递时,在初始调用CTFontManagerRegisterFontsWithAssetNames
函数时,将要求用户将字体安装到系统中,这不是我真正需要的。如果范围是.process
或.none
,则返回与问题末尾提供的相同的errors
输出。
虽然这个函数不符合我的需要,但我至少验证了它是工作的。也许有人会觉得有用。