uifont-Dynamic Download System-supplied fonts-Official code

Source: Internet
Author: User

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

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.