iOS 13 Custom Fonts download and installation

0 votes

THIS IS NOT A DUPLICATE QUESTION

I have searched for everything that I can, but I didn't find something meaningful.

Here is the question:

I want to download a font using CoreText API but it works in iOS 12, NOT available on iOS 13.

There is a demo project from Apple in 2013. But The API it used seems to be private since iOS 13.

/*     
    @param      progressBlock
Callback block to indicate the progress.
Return true to continue, and return false to cancel the process.
This block is called on a private serial queue on OS X 10.15, iOS 13, and later.
     
*/

@available(iOS 6.0, *)
public func CTFontDescriptorMatchFontDescriptorsWithProgressHandler(_ descriptors: CFArray, _ mandatoryAttributes: CFSet?, _ progressBlock: @escaping CTFontDescriptorProgressHandler) -> Bool

As the documentation declared:

This block is called on a private serial queue on OS X 10.15, iOS 13, and later.

Here is my code for font download:

let fontName = "STXingkai-SC-Light"
let attributes = [kCTFontNameAttribute : fontName] as CFDictionary
let fontDescription = CTFontDescriptorCreateWithAttributes(attributes)
// create font font descriptor and add it in an array
let descs = [fontDescription] as CFArray
CTFontDescriptorMatchFontDescriptorsWithProgressHandler(descs, nil) { (state, progressParamater) -> Bool in
    let progressValue = (progressParamater as Dictionary)[kCTFontDescriptorMatchingPercentage]?.doubleValue
    
    switch state {
    case .didBegin: print("didBegin")
    case .didFinish: print("didFinish")
    case .willBeginDownloading: print("willBeginDownloading")
    case .didFinishDownloading:
        print("--> download finish")
        DispatchQueue.main.async {
            self.fontLabel.font = UIFont(name: self.fontName, size: 20)
        }
    case .downloading:
        print("downloading#####\(progressValue ?? 0.0)")
        DispatchQueue.main.async {
            self.progressView.progress = Float(progressValue ?? 0.0)
        }
    case .didFailWithError:
        if let error = (progressParamater as Dictionary)[kCTFontDescriptorMatchingError] as? NSError {
            print(error.description)
        } else {
            print("ERROR MESSAGE IS NOT AVAILABLE")
        }
        
    default: print(String(reflecting: state))
    }
    
    return true
    
}

I tried this API on iOS 12 and Xcode 10, everything works fine and I can download a font with provided fontName.

font download successfully

But, when I use this one on Xcode 11 Version 11.0 beta 6 (11M392q) and macOS Catalina 10.15 Beta (19A526h), something went wrong and that method is no longer effective. And I got some output:

didBegin
__C.CTFontDescriptorMatchingState
didFinish
done
"Error Domain=com.apple.CoreText.CTFontManagerErrorDomain Code=303 \"0 
font descriptors do not have information to specify a font file.\"
UserInfo={NSLocalizedDescription=0 
font descriptors do not have information to specify a font file.

0 font descriptors do not have information to specify a font file

The state will only have didBegin and didFinish, not calling downloading.

WWDC19 seeesion 227 has announced that CTFontManagerRegisterFontDescriptors(fontDescriptors:scope:enabled:registrationHandler:) is available and something about font management. But the source code from the session pdf is not clearly and I've tried, got an other error:

done
[Error Domain=com.apple.CoreText.CTFontManagerErrorDomain Code=306 "The file is not in an allowed location. It must be either in the application’s bundle or an on-demand resource." UserInfo={NSLocalizedDescription=The file is not in an allowed location. It must be either in the application’s bundle or an on-demand resource., CTFontManagerErrorFontURLs=(
    "http://iweslie.com/fonts/HanziPenSC-W3.ttf"
), NSLocalizedFailureReason=Font registration was unsuccessful.}]
done

Here is my code referencing from WWDC19 session 227:

let urlString = "http://iweslie.com/fonts/HanziPenSC-W3.ttf"
let url = URL(string: urlString)! as CFURL
let fontURLArray = [url] as CFArray
CTFontManagerRegisterFontURLs(fontURLArray, .persistent, true) { (errors, done) -> Bool in
    if done {
        print("done")
    }
    print(errors as Array)
    return true
}

And got the error:

Error Domain=com.apple.CoreText.CTFontManagerErrorDomain Code=306

"The file is not in an allowed location. It must be either in the application’s bundle or an on-demand resource."


 

Nov 22, 2022 in Mobile Development by gaurav
• 23,260 points
2,283 views

1 answer to this question.

0 votes
  1. Step 1: From the Home screen, tap Settings to open the iOS settings app menu. 
  2. Step 2: While on the settings menu, find and then tap General. 
  3. Step 3: Scroll to find and then tap Fonts. 
  4. Step 4: To add a new font, tap the option to Open App Store.
answered Dec 15, 2022 by gaurav
• 23,260 points

Related Questions In Mobile Development

0 votes
0 answers

How to receive single tap and hold Remote-Control Event with iOS

I'm looking for my app to listen ...READ MORE

Nov 8, 2022 in Mobile Development by gaurav
• 23,260 points
211 views
0 votes
0 answers

iOS Custom Notification Sound (UNNotificationSound) doesn't play when iPhone is locked

My app uses remote notifications with a ...READ MORE

Nov 9, 2022 in Mobile Development by gaurav
• 23,260 points
797 views
0 votes
0 answers

Xcode(version 13.3.1) doesn’t support iOS 15.6

I updated my iPhone to iOS 15.6 ...READ MORE

Nov 10, 2022 in Mobile Development by gaurav
• 23,260 points
401 views
0 votes
0 answers

Tab Bar Appearances before ios 13

I have upgraded to Xcode 13, I'm ...READ MORE

Nov 10, 2022 in Mobile Development by gaurav
• 23,260 points
316 views
0 votes
1 answer

Get Font.Weight in iOS 14

This is not a bug. It is ...READ MORE

answered Dec 15, 2022 in Mobile Development by gaurav
• 23,260 points
715 views
0 votes
1 answer

Any rate limiter for kitura

Try using KituraLimiter as a rate limiting ...READ MORE

answered Jun 4, 2018 in DevOps Tools by DareDev
• 6,890 points
490 views
0 votes
1 answer

Problem with Swift API Gateway key authorizatiion Ask

Try to add all the headers that ...READ MORE

answered Jun 12, 2018 in AWS by Cloud gunner
• 4,670 points
570 views
0 votes
1 answer
0 votes
1 answer

Flutter different theme for Android and iOs

You can easily override theme by wrapping ...READ MORE

answered Nov 25, 2022 in Mobile Development by gaurav
• 23,260 points
466 views
0 votes
1 answer

How to create custom Document Icon in iOS 14+ apps

Tap Add to Home Screen and select ...READ MORE

answered Dec 15, 2022 in Mobile Development by gaurav
• 23,260 points
424 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP