How to Register Font which was pick up from UIDocument

183 Views Asked by At

I'm trying to Register Font which was pick up from UIDocument, everything works fine

I enabled Install Fonts capability under the Signing & Capabilities and I accessed the UIDocument file and I pick the font. but I don't know how to register it.

This is my code

@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

There are 1 best solutions below

0
Faris On

I figured it out at last and this is the answer for other people looking for a solution.

 @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")
        }
    }