iOS Font Download

Source: Internet
Author: User

Combined with a book and Apple official examples, summarize the download method.

Apple gives us a lot of beautiful fonts, but some font devices are not built-in, we need to download.

The font name that the system provides to us can be consulted through the font book provided by the Mac system.

Once we get the fonts we want, we can download them on our device. To say this, the device fonts are available for all applications after they are downloaded, and the font directory is not the directory of our app, so it does not increase the space required for our application.

Here is a brief example of Apple's official example (official case):

In the case we have reserved several fonts for us to download

1- (void) Viewdidload2 {3 [Super Viewdidload];4     5Self.fontnames =[[Nsarray alloc] Initwithobjects:6                         @"Stxingkai-sc-light",7                         @"dfwawasc-w5",8                         @"fzltxhk--gbk1-0",9                         @"Stlibian-sc-regular",Ten                         @"Liheipro", One                         @"HIRAGINOSANSGB-W3", A Nil]; -Self.fontsamples =[[Nsarray alloc] Initwithobjects: -                         @"Chinese writing Information Technology standard phase", the                         @"easy to download user-friendly file", -                         @"Support Service Upgrade Information professional system", -                         @"make creative Space fast wireless internet", -                         @"兙 兛 兞 兝 兡 兣 嗧 kw 糎", +                         @"㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩", - Nil]; +}

The method of validating the font is then invoked in the Didselectedrowatindexpath delegate of the representation diagram

 1 -(void ) TableView: (UITableView * ) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath  2  { 3   4  //
      Dismiss the keyboard in the text view if it is currently displayed  6  if   ([Self.ftextview Isfirstresponder ])  7   [Self.ftextview Resignfirstresponder];  8 } 

To focus on the Asynchronouslysetfontname method, first verify that the font is present

uifont* Afont = [Uifont fontwithname:fontname size: A.]; //If The font is already downloaded    if(Afont && ([afont.fontname compare:fontname] = = Nsorderedsame | | [Afont.familyname compare:fontname] = =nsorderedsame)) {        //Go ahead and display the sample text.Nsuinteger Sampleindex =[_fontnames Indexofobject:fontname]; _ftextview.text=[_fontsamples Objectatindex:sampleindex]; _ftextview.font= [Uifont fontwithname:fontname size: -.]; return; }

If there is no change to the font, then Afont will return nil to not execute the judgment statement, and if so, use it directly and return it.

Next look at how the font is downloaded when it doesn't exist:

1     //Create A dictionary with the font ' s PostScript name.2Nsmutabledictionary *attrs =[Nsmutabledictionary dictionarywithobjectsandkeys:fontname, Kctfontnameattribute, nil];3     4     //Create A new font descriptor reference from the attributes dictionary.5Ctfontdescriptorref desc =ctfontdescriptorcreatewithattributes ((__bridge cfdictionaryref) attrs);6     7Nsmutablearray *descs = [Nsmutablearray arraywithcapacity:0];8[Descs AddObject: (__bridgeID) desc];9 cfrelease (DESC);Ten      One__block BOOL errorduringdownload = NO;

First configure the properties that we need to download the font, and put FontName as the value Kctfontnameattribute as the key into the dictionary.

Then use Ctfontdescriptorcreatewithattribute to create a font descriptor and pass nsdictionary to Cfdictionaryref as a parameter.

Put the ctfontdescriptorref into the array (you also need to convert to an object).

Next you need to call

Ctfontdescriptormatchfontdescriptorswithprogresshandler ((__bridge cfarrayref) Descs, NULL, ^ ( Ctfontdescriptormatchingstate state, Cfdictionaryref Progressparameter)

To determine if the font has been matched, the first parameter gives us a description of the font array, the second one is set to NULL, and the third parameter is the callback block.

The state parameter of the block is the current matching status, Progressparmeter is the progress parameter, which also contains the error message.

The state we use has the following:

Kctfontdescriptormatchingdidbegin//Start matching

Kctfontdescriptormatchingdidfinish//Match success

kctfontdescriptormatchingwillbegindownloading//font Start download

kctfontdescriptormatchingdidfinishdownloading//Font Download Successful

kctfontdescriptormatchingdownloading//download

Kctfontdescriptormatchingdidfailwitherror//Match failed

The callback order for these states is

Kctfontdescriptormatchingdidbegin

Kctfontdescriptormatchingwillbegindownloading

Kctfontdescriptormatchingdownloading (Multiple calls)

Kctfontdescriptormatchingdidfinishdownloading

Kctfontdescriptormatchingdidfinish

Where kctfontdescriptormatchingdownloading will be called multiple times to allow us to update the download progress bar.

Without a callback, we can do the UI processing, block or use notifications.

Progressparmeter we used two properties.

Because it's cfdictionaryref, we should first turn it into nsdictionary.

The key for the current download progress is kctfontdescriptormatchingpercentage

The key for the Error object is Kctfontdescriptormatchingerror

If the returned state is Kctfontdescriptormatchingdidfailwitherror, then we can get the error log by Kctfontdescriptormatchingerror.

iOS Font Download

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.