资产目录中字体资产的定义是什么?

从 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)?)

但是,Asset Catalog 没有任何方法可以添加“字体资产”。

我尝试过的:

  • 采用字体 KanitRegular.ttf(PostScript 名称为 Kanit-Regularhere
  • 在资产目录中创建了名为 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
    }
}

此后,打印 errors

▿ 1 element
  - 0 : Error Domain=NSPOSIXErrorDomain Code=22 "Invalid argument" UserInfo={CTFontManagerErrorFontAssetNameKey=(
    "Kanit-Regular"
)}

有什么办法让它工作吗?

a423355b20681 回答:资产目录中字体资产的定义是什么?

通过执行以下操作使其正常工作:

  • 使用 Kanit-Regular 按需资源标签标记 fonts 数据资产(标签名称可以是您的偏好名称,fonts 只是示例)。
  • fonts 标签放入 Initial Install Tags 预取资源标签部分

Initial Install Tags Resource Tags section

  • Fonts 中添加 Signing & Capabilities 功能并勾选其中的所有框

enter image description here

  • 在注册字体前实现捆绑资源访问请求

像这样:

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 输出。

虽然这个功能不符合我的需求,但我至少验证了它是有效的。也许有人会觉得它很有用。

本文链接:https://www.f2er.com/770827.html

大家都在问