一,工程目录

二,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开发进阶》 -唐巧

 
 

【读书笔记】iOS-UIFont-动态下载系统提供的字体-官方代码的更多相关文章

  1. 【读书笔记】iOS-UIFont-动态下载系统提供的多种中文字体网址

    苹果可使用的字体列表: https://support.apple.com/zh-cn/HT202599 动态下载字体的代码demo: https://developer.apple.com/libr ...

  2. UIFontDownLoad ----动态下载系统提供的字体

    程序运行结果如下 : 当点击对应单元格实现下载对应的字体. 控制台打印结果如下 : 2015-10-05 11:14:04.132 UIFontDownLoad[12721:86827] state ...

  3. iOS 动态下载系统提供的中文字体

    使用系统提供的中文字体,既可避免版权问题,又可以减小应用体积 #pragma mark - 判断字体是否已经被下载 - (BOOL)isFontDownLoaded:(NSString *)fontN ...

  4. [读书笔记]Java之动态分派

    以下内容来自周志明的<深入理解Java虚拟机>. 前一篇说了静态分派和重载有关,现在的动态分派就和覆盖Override有关了. 先看代码: public class DynamicDisp ...

  5. [读书笔记]iOS 7 UI设计 对比度

    好久没写随笔了,最近在读<iOS 7 byTutorials>,很不错,推荐给大家. 每一个好的程序员也都是一个设计师,不懂设计的程序员不是好的CTO.哈哈,开个小玩笑. iOS 7设计的 ...

  6. $《第一行代码:Android》读书笔记——第1章 Android系统

    (一)Android系统架构 1.Linux内核层:各种底层驱动,如显示驱动.音频驱动.电源管理等. 2.系统运行库层:各种库支持,如3D绘图.浏览器内核.数据库等. 3.应用框架层:各种API,各种 ...

  7. 读书笔记-iOS核心动画高级技巧

    如果不使用+imageNamed:,那么把整张图片绘制到CGContext可能是最佳的方式了. 这里我们利用了CALayer的KVC来存储和检索任意的值,将图层和索引打标签. 使用KVC打标签

  8. 【读书笔记】【深入理解ES6】#13-用模块封装代码

    什么是模块 模块是自动运行在严格模式下并且没有办法退出运行的 JavaScript 代码. 在模块顶部创建的变量不会自动被添加到全局变量作用域,这个变量仅在模块的顶级作用域中存在,而且模块必须导出一些 ...

  9. 《实战java高并发程序设计》源码整理及读书笔记

    日常啰嗦 不要被标题吓到,虽然书籍是<实战java高并发程序设计>,但是这篇文章不会讲高并发.线程安全.锁啊这些比较恼人的知识点,甚至都不会谈相关的技术,只是写一写本人的一点读书感受,顺便 ...

随机推荐

  1. MyEclipse8.5集成Tomcat7时的启动错误:Exception in thread “main” java.lang.NoClassDefFoundError org/apache/commons/logging/LogFactory

    今天,安装Tomcat7.0.21后,单独用D:\apache-tomcat-7.0.21\bin\startup.bat启动web服务正常.但在MyEclipse8.5中集成配置Tomcat7后,在 ...

  2. 【HTML】Iframe中的onload事件

    当iframe.src重新指定一个url时会重新执行iframe的onload事件 <iframe id="indexFrame" name="index" ...

  3. 常用CSS Reset汇总

    什么是Css Reset呢? 在 HTML标签在浏览器里有默认的样式,不同浏览器的默认样式之间也会有差别.在切换页面的时候,浏览器的默认样式往往会给我们带来麻烦,影响开发效率.所以解决的方法就是一开始 ...

  4. 解决两台centos虚拟机Telnet服务无法联机的问题

    关闭防火墙 [root@localhost ~]# service iptables stopiptables: Flushing firewall rules:                    ...

  5. Android 学习笔记之WebService实现远程调用+内部原理分析...

    PS:终于可以抽出时间写写博客了,忙着学校的三周破实训外加替考...三周了,没怎么学习...哎... 学习内容: 1.WebService 实现远程方法的调用   什么是WebService...   ...

  6. Android 学习笔记之SurfaceView的使用+如何实现视频播放...

    学习内容: 1.掌握Surface的使用... 2.Android中如何实现视频播放... 1.SurfaceView类的使用   在Android中,一般播放音频时我们可以去使用Android提供的 ...

  7. [Solution] 一步一步WCF(2) 终结点Endpoint

    繁忙的一天又一天,不管其他,先继续WCF吧. Endpoint包含地址,绑定,契约三要素.WCF作为一个Windows平台下最大的通信框架.通过终结点承载了所有通信功能.所以终结点的作用将非常重要. ...

  8. 实例对比剖析c#引用参数的用法

    c#引用参数传递的深入剖析值类型的变量存储数据,而引用类型的变量存储对实际数据的引用.(这一点很重要,明白了之后就能区分开值类型和引用类型的差别) 在参数传递时,值类型是以值的形式传递的(传递的是值, ...

  9. windbg学习进阶之——dump分析常用命令收集

    #重要说明 (1) windbg命令分为标准命令,元命令和扩展命令. 标准命令提供最基本的调试功能,不区分大小写.如:bp  g  dt  dv  k等 元命令提供标准命令没有提供的功能,也内建在调试 ...

  10. 别误用IsDigit与IsNumber函数

    1.起因 最近发现程序中有一段控制TextBox数字输入的代码,相信大家都不会太陌生,如下: void int_KeyPress(object sender, KeyPressEventArgs e) ...