如何注册从 UIDocument 获取的字体

Posted

技术标签:

【中文标题】如何注册从 UIDocument 获取的字体【英文标题】:How to Register Font which was pick up from UIDocument 【发布时间】:2020-02-14 23:24:56 【问题描述】:

我正在尝试注册从 UIDocument 获取的字体,一切正常

I enabled Install Fonts capability under the Signing & CapabilitiesI accessed the UIDocument file and I pick the font. 但我不知道如何注册。

这是我的代码

@objc func didPressButton(_ sender: UIButton) 
    let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.truetype-ttf-font", "public.opentype-font"], in: UIDocumentPickerMode.import)
     documentPicker.delegate = self
    self.present(documentPicker, animated: true, completion: nil)


// MARK: - UIDocumentPickerDelegate Methods
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) 

    let fontUrl = Bundle(for: type(of : self)).path(forResource: "\(urls[0].path)", ofType: nil)
   CTFontManagerRegisterFontURLs([fontUrl] as CFArray, .persistent, true)  (errors, done) -> Bool in
       if(done) 
           print("Done")
       
       print(errors as Array)
    for family in UIFont.familyNames.sorted() 
        let names = UIFont.fontNames(forFamilyName: family)
        print("Family: \(family) Font names: \(names)")
    

       return true
   


 func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) 
    controller.dismiss(animated: true, completion: nil)

【问题讨论】:

【参考方案1】:

我终于想通了,这是寻找解决方案的其他人的答案。

 @objc func didPressButton(_ sender: UIButton) 
        let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.truetype-ttf-font", "public.opentype-font"], in: UIDocumentPickerMode.import)
         documentPicker.delegate = self
        self.present(documentPicker, animated: true, completion: nil)
    

    // MARK: - UIDocumentPickerDelegate Methods
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) 

       self.handleFileSelection(inUrl: urls.first!)

    

     func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) 
        controller.dismiss(animated: true, completion: nil)
    



    private func handleFileSelection(inUrl:URL) -> Void 
        do 
         // inUrl is the document's URL
            let fontData = try Data(contentsOf: inUrl) // Getting file data here
            let dataProvider = CGDataProvider(data: fontData as CFData)
            let cgFont = CGFont(dataProvider!)

                var error: Unmanaged<CFError>?
            if !CTFontManagerRegisterGraphicsFont(cgFont!, &error)
                
                    print("Error loading Font!")
                 else 

                let fontName = cgFont!.postScriptName

                self.lab.font = UIFont(name: String(fontName!), size: 30)
                for family in UIFont.familyNames.sorted() 
                    let names = UIFont.fontNames(forFamilyName: family)
                    print("Family: \(family) Font names: \(names)")
                
            

         catch 
            print("document loading error")
        
    

【讨论】:

以上是关于如何注册从 UIDocument 获取的字体的主要内容,如果未能解决你的问题,请参考以下文章

iOS 5 中关于 UIDocument 的说明

在没有 UIDocument 的情况下获取 iCloud URL 的数据内容

如何在主队列上加载 UIDocument?

打开 UIDocument 时如何显示错误消息?

同步打开 UIDocument

当应用程序在后台时,如何检查我的 UIDocument 是不是已通过 iCloud 更新?