国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

【讀書筆記】UIFont-動態下載系統提供的字體-官方代碼

2019-11-14 18:36:20
字體:
來源:轉載
供稿:網友

一,工程目錄

 

 

二,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開發進階》 -唐巧

 

 
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荔波县| 原平市| 离岛区| 汶川县| 广安市| 福贡县| 衡阳市| 汪清县| 达州市| 商洛市| 马边| 古浪县| 崇礼县| 彭阳县| 盐山县| 临清市| 镇康县| 台南县| 邢台市| 莆田市| 麻阳| 富锦市| 黄平县| 乌什县| 余姚市| 阿克陶县| 登封市| 南丰县| 林州市| 南通市| 平潭县| 股票| 武冈市| 黑山县| 台南市| 龙川县| 花莲市| 前郭尔| 建昌县| 关岭| 南充市|