簡體   English   中英

資產目錄中字體資產的定義是什么?

[英]What's the definition of Font Asset in the Asset Catalog?

從iOS 13開始, CTFontManager有如下function:

@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 (PostScript 名稱是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
    }
}

在此之后,打印errors

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

有什么辦法讓它工作嗎?

通過執行以下操作使其工作:

  • 使用fonts按需資源標簽標記Kanit-Regular數據資產(標簽名稱可以是您的偏好名稱, fonts只是示例)。
  • fonts標簽放入Initial Install Tags Prefetched Resource Tags 部分

初始安裝標簽資源標簽部分

  • Signing & Capabilities中添加Fonts Capability 並勾選其中的所有框

在此處輸入圖像描述

  • 在注冊fonts之前實現bundle資源訪問請求

像這樣:

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
            }
        }
    }
}

結果

當scope被以.persistent.user的方式傳遞,以CTFONTMANAGERREGISTER CTFontManagerRegisterFontsWithAssetNames 0140年8月8日。 如果 scope 是.process.none ,則返回問題末尾提供的相同errors output 。

雖然這個 function 不符合我的需要,但我至少驗證了它正在工作。 也許有人會發現它很有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM