一,工程目錄

二,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;}
三,ViewController.h
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{ UITableView *myTableView; NSArray *fontNames; NSArray *fontSamples;}@end
四, 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. //初始化數據 [self addData]; //初始化界面 [self addView]; }#PRagma -mark -functions//初始化界面-(void)addView{ myTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 100, 320, 200)]; myTableView.delegate=self; myTableView.dataSource=self; [self.view addSubview:myTableView];}//初始化數據-(void)addData{ fontNames = [[NSArray alloc] initWithObjects: @"STXingkai-SC-Light", @"DFWaWaSC-W5", @"FZLTXHK--GBK1-0", @"STLibian-SC-Regular", @"LiHe字體開始進行下載- (void)asynchronouslySetFontName:(NSString *)fontName{ UIFont* aFont = [UIFont fontWithName:fontName size:12.]; //判斷字體是否已經被下載 if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame)) { NSLog(@"字體已經被下載"); return; } //用字體的PostScript名字創建一個Dictionary NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil]; // 創建一個字體描述對象CTFontDescriptorRef CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs); //將字體描述對象放到一個NSMutableArray中 NSMutableArray *descs = [NSMutableArray arrayWithCapacity:0]; [descs addObject:(__bridge id)desc]; CFRelease(desc); __block BOOL errorDuringDownload = NO; //開始對字體進行下載 CTFontDescriptorMatchFontDescriptorsWithProgressHandler( (__bridge CFArrayRef)descs, NULL, ^(CTFontDescriptorMatchingState state, CFDictionaryRef progressParameter) { NSLog( @"state %d - %@", state, progressParameter); double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue]; if (state == kCTFontDescriptorMatchingDidBegin) { dispatch_async( dispatch_get_main_queue(), ^ { NSLog(@"字體已經匹配"); }); } else if (state == kCTFontDescriptorMatchingDidFinish) { dispatch_async( dispatch_get_main_queue(), ^ { NSLog(@"字體下載完成"); // 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(@"字體開始下載"); }); } else if (state == kCTFontDescriptorMatchingDidFinishDownloading) { dispatch_async( dispatch_get_main_queue(), ^ { NSLog(@"字體下載完成"); }); } else if (state == kCTFontDescriptorMatchingDownloading) { dispatch_async( dispatch_get_main_queue(), ^ { NSLog(@"下載進度"); }); } else if (state == kCTFontDescriptorMatchingDidFailWithError) { NSLog(@"下載失敗"); 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
參考資料:《iOS開發進階》 -唐巧
新聞熱點
疑難解答