One, engineering catalogue
Two, APPDELEGATE.M
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions { Override point for customization after application launch. Viewcontroller *view=[[viewcontroller Alloc]init]; Uinavigationcontroller *nav=[[uinavigationcontroller Alloc]initwithrootviewcontroller:view]; Self.window.backgroundcolor=[uicolor Whitecolor]; Self.window.rootviewcontroller=nav; return YES;}
Three, ViewController.h
#import <UIKit/UIKit.h> @interface Viewcontroller:uiviewcontroller<uitableviewdelegate, uitableviewdatasource>{ UITableView *mytableview; Nsarray *fontnames; Nsarray *fontsamples;} @end
Four, VIEWCONTROLLER.M
#import "ViewController.h" #import <CoreText/CoreText.h> @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload]; Additional setup after loading the view, typically from a nib. Initialize data [self addData]; Initialize interface [self addview]; } #pragma-mark-functions//initialization interface-(void) addview{Mytableview=[[uitableview alloc]initwithframe:cgrectmake (0, 100, 320 , 200)]; mytableview.delegate=self; mytableview.datasource=self; [Self.view Addsubview:mytableview];} Initialize data-(void) adddata{fontnames = [[Nsarray alloc] initwithobjects: @ "Stxingkai-sc-light", @ "Dfwawasc-w5", @ "fzltxhk--gbk1-0", @ "Stlibian-sc-regular", @ "LIHEIPR O ", @" HIRAGINOSANSGB-W3 ", nil]; Fontsamples = [[Nsarray alloc] initwithobjects: @ "Chinese writing information Technology standard phase", @ "File download user interface is simple", @ "Support Service upgrade information professional system"@ "Fast wireless internet for creative space" @ "兙 兛 兞 兝 兡 兣 嗧 kw 糎", @ "㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩", nil ];} #pragma-mark-uitableviewdelegate-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{return [FontNames count];} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static NSString *myidentifier = @ "Myidentifier"; UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:myidentifier]; if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:myiden Tifier]; } Cell.textLabel.text = Fontnames[indexpath.row]; return cell;} -(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{[self Asynchronouslysetfontname:fontnames[indexpath.row]];} #pragma-mark-functions//font starts to download-(void) Asynchronouslysetfontname: (NSString *) fontname{uifont* Afont = [Uifont fontwithname:fontname Size:12.]; Determine if the font has been downloaded if (Afont && ([afont.fontname compare:fontname] = = Nsorderedsame | | [Afont.familyname compare:fontname] = = nsorderedsame) {NSLog (@ "Font has been downloaded"); Return }//Create a dictionary nsmutabledictionary *attrs with the PostScript name of the font = [Nsmutabledictionary dictionarywithobjectsandkey S:fontname, Kctfontnameattribute, nil]; Create a font description object Ctfontdescriptorref ctfontdescriptorref desc = ctfontdescriptorcreatewithattributes ((__bridge CFDICTIONARYREF) attrs); Place the font description object in a nsmutablearray nsmutablearray *descs = [Nsmutablearray arraywithcapacity:0]; [Descs AddObject: (__bridge ID) desc]; Cfrelease (DESC); __block BOOL errorduringdownload = NO; Start downloading Fonts Ctfontdescriptormatchfontdescriptorswithprogresshandler ((__bridge cfarrayref) Descs, NULL, ^ ( Ctfontdescriptormatchingstate state, Cfdictionaryref progressparameter) {NSLog (@ ' state%d-%@ ', state, p RogressparameteR); Double Progressvalue = [[(__bridge nsdictionary *) Progressparameter Objectforkey: (ID) Kctfontdescriptormatchingpercentage] Doublevalue]; if (state = = Kctfontdescriptormatchingdidbegin) {Dispatch_async (Dispatch_get_main_queue (), ^ { NSLog (@ "Font has been matched"); }); } else if (state = = Kctfontdescriptormatchingdidfinish) {Dispatch_async (Dispatch_get_main_queue (), ^ { NSLog (@ "Font Download complete"); Log the font URL in the console ctfontref fontref = Ctfontcreatewithname ((__bridge cfstringref) FontName, 0., NULL); Cfstringref Fonturl = Ctfontcopyattribute (Fontref, Kctfonturlattribute); Cfrelease (Fonturl); Cfrelease (FONTREF); if (!errorduringdownload) {NSLog (@ "%@ downloaded", fontname); } }); } else if (state = = KCTFONTDESCRIPTORMATChingwillbegindownloading) {Dispatch_async (Dispatch_get_main_queue (), ^ {NSLog (@ "font start download"); }); } else if (state = = kctfontdescriptormatchingdidfinishdownloading) {Dispatch_async (Dispatch_get_main_queue (), ^ {NSLog (@ "Font Download complete"); }); } else if (state = = kctfontdescriptormatchingdownloading) {Dispatch_async (Dispatch_get_main_queue (), ^ { NSLog (@ "Download progress"); }); } else if (state = = Kctfontdescriptormatchingdidfailwitherror) {NSLog (@ "Download Failed"); Nserror *error = [(__bridge nsdictionary *) Progressparameter Objectforkey: (ID) kctfontdescriptormatchingerror]; if (Error! = nil) {NSLog (@ "errormessage--%@-", [error description]); } else {NSLog (@ "error message is not available"); } errorduringdownload = YES; Dispatch_async (Dispatch_get_main_queue (), ^ {NSLog (@ "Download error:%@", [error description]); }); } return (bool) YES; }); }-(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Reference: iOS Development advanced-Tang Qi
uifont-Dynamic Download System-supplied fonts-Official code