UIFont-動態下載系統提供的字型-官方代碼,uifont-下載系統
一,工程目錄
二,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", @"LiHeiPro", @"HiraginoSansGB-W3", nil]; fontSamples = [[NSArray alloc] initWithObjects: @"漢體書寫資訊技術標準相", @"容檔案下載使用介面簡單", @"支援服務升級資訊專業制", @"作創意空間快速無線上網", @"兙兛兞兝兡兣嗧瓩糎", @"㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩", 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:MyIdentifier]; } cell.textLabel.text = fontNames[indexPath.row]; return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [self asynchronouslySetFontName:fontNames[indexPath.row]];}#pragma -mark -functions//字型開始進行下載- (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開發進階》 -唐巧