UIFontDownLoad ----动态下载系统提供的字体
?
程序運行結果如下 :
當點擊對應單元格實現下載對應的字體.
?
控制臺打印結果如下 :
2015-10-05 11:14:04.132 UIFontDownLoad[12721:86827] state 0 - {
}
2015-10-05 11:14:04.134 UIFontDownLoad[12721:86827] state 7 - {
??? CTFontDescriptorMatchingResult =???? (
??????? "UICTFontDescriptor <0x7fddd05388d0> = {\n??? NSFontNameAttribute = Helvetica;\n??? NSFontSizeAttribute = 12;\n}"
??? );
??? CTFontDescriptorMatchingSourceDescriptor = "UICTFontDescriptor <0x7fddd0797a80> = {\n??? NSFont = \"STXingkai-SC-Light\";\n}";
}
2015-10-05 11:14:04.134 UIFontDownLoad[12721:86827] state 1 - {
??? CTFontDescriptorMatchingResult =???? (
??????? "UICTFontDescriptor <0x7fddd05388d0> = {\n??? NSFontNameAttribute = Helvetica;\n??? NSFontSizeAttribute = 12;\n}"
??? );
}
2015-10-05 11:14:04.147 UIFontDownLoad[12721:85405] 字體已經匹配
2015-10-05 11:14:04.147 UIFontDownLoad[12721:85405] 字體下載完成
2015-10-05 11:14:05.562 UIFontDownLoad[12721:85405] STXingkai-SC-Light downloaded
程序實現的代碼如下 :
//
//? ViewController.m
//? UIFontDownLoad
//
//? Created by mac1 on 15/10/5.
//? Copyright (c) 2015年 www.iphonetrain.com. All rights reserved.
//
#import "ViewController.h"
/*
?一種能夠對文本格式和文本布局進行精細控制的文本引擎
?*/
#import <CoreText/CoreText.h>
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)NSArray *fontNames;
@property (nonatomic,strong)NSArray *fontSamples;
@property (nonatomic,strong)UITableView *myTableView;
@end
static NSString *identify = @"cell";
@implementation ViewController
- (void)viewDidLoad {
??? [super viewDidLoad];
? ?
?
??? self.view.backgroundColor = [UIColor greenColor];
?? ?
??? [self addData];
??? [self creatUI];
?? ?
?? ?
?? ?
}
//初始化界面
- (void)creatUI
{
??? _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
??? _myTableView.dataSource = self;
??? _myTableView.delegate = self;
??? [self.view addSubview:_myTableView];
?? ?
?? ?
??? /*
???? 注冊單元格
??? [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identify];
???? */
?? ?
??? ?
}
//初始化數據
- (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];
}
//行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
??? return _fontNames.count;
?? ?
}
//創建單元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
??? //直接顯示內容就不注冊單元格
??? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
??? if (cell == nil) {
??????? cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
?????? ?
??? }
??? 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,kCTFontAttributeName, 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;
??? });
?? ??
}
轉載于:https://www.cnblogs.com/pengsi/p/4855491.html
總結
以上是生活随笔為你收集整理的UIFontDownLoad ----动态下载系统提供的字体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡有效期格式转换为MMYY、YYMM
- 下一篇: 股票策略社群实盘展示