Dynamically registering fonts at the iOS runtime

Source: Internet
Author: User

In iOS development, using third-party fonts, such as TTF files, OTF files, is very simple. Only two steps are required. 1. ttf file, the OTf file is added to the project root directory. 2, add font file in Info.plist, is font filename. When used, double-click to open the font file, the title will display the font name, using the name of the body (setfontname). In Android, the font file is copied to the asset directory, and then the typeface class is used to get its instance based on the file.

The problem is that in the development of our play graph, because the Chinese font is too large, need to download from the line, downloaded to local use, which involves a registration font problem. iOS app gramming will automatically read the Info.plist file when it starts, and if it finds the corresponding font file in the current directory, it will register the font with the system before it can be used according to the font name. So in the font online download this feature, it is not feasible to register fonts in advance in infp.plist, it is necessary to register the font dynamically, is the runtime download the font TTF file, downloaded to the documents directory, and then according to the font file full file name (ttffilename) to register. The following is the key code. 、

  

Nsurl *url = [Nsurl fileurlwithpath:ttffilename];

Cgdataproviderref Fontdataprovider = Cgdataprovidercreatewithurl ((__bridge cfurlref) URL);

if (Fontdataprovider = = NULL)

return Ttffilename;

Cgfontref NewFont = Cgfontcreatewithdataprovider (Fontdataprovider);

Cferrorref error;

if (! Ctfontmanagerregistergraphicsfont (NewFont, &error)) {

Cfstringref errordescription = cferrorcopydescription (Error);

NSLog (@ "Failed to load font:%@", errordescription);

Cfrelease (errordescription);

}

Cfrelease (NewFont);

Cfrelease (Fontdataprovider);

This is the code that is dynamically registered based on the full file name of the font file. Android does not have a registration issue, because it is based on the font file full file name to the provincial typeface instance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.