How iOS uses custom fonts (built-in and any download of TTF\OTF\TTC font files)

Source: Internet
Author: User

Recently, I've done a reading application and used a custom font to learn a bit about it.

1. First is the simplest and most common practice, packed with built-in Word repertoires files:

Add a font library file to the project, such as Font1.ttf add to the project, and then add a fonts provided by application in the project plist, this is the array, and then add the key item1,value is just said Font1.ttf,

Then you can use the font directly in the project.

+ (Uifont *) Fontwithname: (nsstring *) fontname size: (cgfloat) fontSize;

However, it is important to note that this fontname is not a filename, but a real font name inside . If the font in the Font1.ttf above is mfqingshu_noncommercial-regular, then use it directly

Uifont *font = [uifont fontwithname: @ "mfqingshu_noncommercial-regular" size: 12]; You can go to the right font.

2. However, in general, font files are larger, should not be built-in, and if you use plist predefined way, it will certainly not be able to cover the whole, so that users can not use more of their favorite fonts. So the way the font should be read in code:

Provide the font file path and return the font you want:

-(uifont*) Customfontwithpath: (nsstring*) path size: (cgfloat) size{    *fonturl = [Nsurl Fileurlwithpath:path];     = Cgdataprovidercreatewithurl ((__bridge cfurlref) fonturl);     = Cgfontcreatewithdataprovider (fontdataprovider);    Cgdataproviderrelease (Fontdataprovider);    Ctfontmanagerregistergraphicsfont (Fontref, NULL);     *fontname = cfbridgingrelease (cgfontcopypostscriptname (Fontref));     *font = [Uifont fontwithname:fontname size:size];    Cgfontrelease (fontref);     return font;}

This will not need to set anything in the plist, just need to get the font library file path, you can remove the corresponding font.

The above method is valid for TTF and OTF fonts, but only one font is removed for TTC fonts. Because the TTC font is a collection of similar fonts, it is generally a combination of fonts. Therefore, if the font requirements are relatively high, so you can use the following methods to take out all the fonts:

-(nsarray*) Customfontarraywithpath: (nsstring*Path Size: (cgfloat) size{cfstringref Fontpath=cfstringcreatewithcstring (NULL, [path utf8string], kCFStringEncodingUTF8); Cfurlref Fonturl= Cfurlcreatewithfilesystempath (NULL, Fontpath, Kcfurlposixpathstyle,0); Cfarrayref Fontarray=Ctfontmanagercreatefontdescriptorsfromurl (Fonturl);    Ctfontmanagerregisterfontsforurl (Fonturl, Kctfontmanagerscopenone, NULL); Nsmutablearray*customfontarray =[Nsmutablearray array];  for(Cfindex i =0; I < Cfarraygetcount (Fontarray); i++) {Ctfontdescriptorref descriptor=Cfarraygetvalueatindex (Fontarray, i); Ctfontref Fontref=Ctfontcreatewithfontdescriptor (descriptor, size, NULL); NSString*fontname =cfbridgingrelease (Ctfontcopyname (Fontref, Kctfontpostscriptnamekey)); Uifont*font =[Uifont fontwithname:fontname size:size];    [Customfontarray Addobject:font]; }        returnCustomfontarray;}

However, this method only supports more than 7.0, temporarily under 7.0 does not find the method.

Personal opinion, because TTC inside the font are more similar, so actually use one is enough.

Attached: (Introduction to Fonts)

TTF (Truetypefont) is a font name. TTF (Truetypefont) is a font file format co-launched by Apple and Microsoft, and has become the most commonly used font file representation with the popularity of Windows.

The TTC font is a TrueType font integration file (. TTC file), which contains multiple fonts in a single file structure to more effectively share contour data, and TTC technology can effectively reduce the size of font files when multiple fonts share the same stroke. TTC is a library of several TTF-synthesized fonts, and you will see more than two fonts in the Font list after installation. Two fonts in most words are the same, you can make a TTC file two fonts, common TTC font, because the sharing of stroke data, so most of the font differences in this collection is only the character width, so as to adapt to different layout requirements. The TTF font, however, contains only one font.

How iOS uses custom fonts (built-in and any download of TTF\OTF\TTC font files)

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.