UIFont-font provided by the dynamic Download System-official code, uifont-Download System
1. project directory
Ii. 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;}
3. ViewController. h
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{ UITableView *myTableView; NSArray *fontNames; NSArray *fontSamples;}@end
4. ViewController. m
# Import "ViewController. h "# import <CoreText/CoreText. h> @ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // initialize data [self addData]; // initialization interface [self addView];} # pragma-mark-functions // initialization interface-(void) addView {myTableView = [[UITableView alloc] initWithFrame: CGRectMake (0,100,320, 20 0)]; 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", @ "LiHeiPro", @ "HiraginoSansGB-W3", nil]; fontSamples = [[NSArray alloc] initWithObjects: @ "Chinese ", @ "simple file sharing download interface", @ "professional information system upgrade for Support Services", @ "quick wireless Internet access for creative spaces", @ "Too many rows have been reached too many rows", @ "(I) (II) (IV) (VI) have been transferred", 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 dequeueReusableCellWithId Entifier: MyIdentifier]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: MyIdentifier];} cell. textLabel. text = fontNames [indexPath. row]; return cell;}-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {[self asynchronouslySetFontName: fontNames [indexPath. row] ;}# pragma-mark-functions // The font starts to download -( Void) asynchronouslySetFontName :( NSString *) fontName {UIFont * aFont = [UIFont fontWithName: fontName size: 12.]; // determine whether the font has been downloaded if (aFont & ([aFont. fontName compare: fontName] = NSOrderedSame | [aFont. familyName compare: fontName] = NSOrderedSame) {NSLog (@ "the font has been downloaded"); return ;} // create a Dictionary NSMutableDictionary * attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys: fontName, KCTFontNameAttribute, nil]; // create a font description object CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes (_ bridge CFDictionaryRef) attrs ); // place the font description object in an NSMutableArray * descs = [NSMutableArray arrayWithCapacity: 0]; [descs addObject :( _ bridge id) desc]; CFRelease (desc ); _ block BOOL errorDuringDownload = NO; // start downloading the font CTFontDescriptorMatchFontDescriptorsWithProgr EssHandler (_ bridge CFArrayRef) descs, NULL, ^ (CTFontDescriptorMatchingState, CFDictionaryRef progressParameter) {NSLog (@ "state % d-% @", state, progressParameter ); double progressValue = [[(_ bridge NSDictionary *) progressParameter objectForKey :( id) updated] doubleValue]; if (state = encrypted) {dispatch_async (dispatch_get_main_queue (), ^ {NSLog (@ "font matched") ;}) ;}else if (state = kCTFontDescriptorMatchingDidFinish) {dispatch_async (dispatch_get_main_queue (), ^ {NSLog (@ "font downloaded"); // Log the font URL in the console CTFontRef fontRef = CTFontCreateWithName (_ bridge CFStringRef) fontName, 0 ., NULL); CFStringRef fontURL = CTFontCopyAttribute (fontRef, kctfonturlattriase); CFRelease (fontURL); CFRelease (fontRef); if (! ErrorDuringDownload) {NSLog (@ "% @ downloaded", fontName) ;}};} else if (state = success) {dispatch_async (dispatch_get_main_queue (), ^ {NSLog (@ "start downloading") ;});} else if (state = kCTFontDescriptorMatchingDidFinishDownloading) {dispatch_async (dispatch_get_main_queue (), ^ {NSLog (@ "font download completed") ;});} else if (state = kCTFontDescriptorMatchingDownloading) {d Ispatch_async (dispatch_get_main_queue (), ^ {NSLog (@ "download progress") ;}) ;} else if (state = progress) {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 that can be recreated .} @ end
References: iOS development advanced-Tang Qiao