结合书本与苹果官方给的例子后,总结下下载的方法。

苹果给我们提供了很多漂亮的字体,只是有些字体设备并没有内置,需要我们去下载才行。

系统提供给我们的字体名我们可以通过mac系统提供的字体册来查阅。

得到我们想要的字体后就可以在我们的设备上进行下载了。这里要说一下,设备字体下载后是所有应用都可以使用的,而且字体的目录并不是我们APP的目录,因此并不会增大我们应用所需的空间。

这里结合着苹果官方所给例子来简述一下(官方例子):

事例中给我们预定了几种字体来让我们下载

 - (void)viewDidLoad
{
[super viewDidLoad]; self.fontNames = [[NSArray alloc] initWithObjects:
@"STXingkai-SC-Light",
@"DFWaWaSC-W5",
@"FZLTXHK--GBK1-0",
@"STLibian-SC-Regular",
@"LiHeiPro",
@"HiraginoSansGB-W3",
nil];
self.fontSamples = [[NSArray alloc] initWithObjects:
@"汉体书写信息技术标准相",
@"容档案下载使用界面简单",
@"支援服务升级资讯专业制",
@"作创意空间快速无线上网",
@"兙兛兞兝兡兣嗧瓩糎",
@"㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩",
nil];
}

然后在表示图的didSelectedRowAtIndexPath委托中来调用验证字体的方法

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self asynchronouslySetFontName:_fontNames[indexPath.row]]; // Dismiss the keyboard in the text view if it is currently displayed
if ([self.fTextView isFirstResponder])
[self.fTextView resignFirstResponder];
}

重点来看asynchronouslySetFontName方法,首先先验证是否存在该字体

UIFont* aFont = [UIFont fontWithName:fontName size:.];
// If the font is already downloaded
if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame)) {
// Go ahead and display the sample text.
NSUInteger sampleIndex = [_fontNames indexOfObject:fontName];
_fTextView.text = [_fontSamples objectAtIndex:sampleIndex];
_fTextView.font = [UIFont fontWithName:fontName size:.];
return;
}

如果不存在改字体,那么aFont将会返回nil不执行该判断语句,如果存在就直接使用并返回。

接下来看看不存在时,是如何进行字体下载的:

     // Create a dictionary with the font's PostScript name.
NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil]; // Create a new font descriptor reference from the attributes dictionary.
CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs); NSMutableArray *descs = [NSMutableArray arrayWithCapacity:];
[descs addObject:(__bridge id)desc];
CFRelease(desc); __block BOOL errorDuringDownload = NO;

首先配置我们需要下载字体的属性,将fontName作为值kCTFontNameAttribute作为键放入字典中。

然后使用CTFontDescriptorCreateWithAttribute来创建一个字体描述器并将NSDictionary转为CFDictionaryRef作为参数传入。

将CTFontDescriptorRef放入数组中(同样需要转为对象)。

接下来需要调用

CTFontDescriptorMatchFontDescriptorsWithProgressHandler( (__bridge CFArrayRef)descs, NULL,  ^(CTFontDescriptorMatchingState state, CFDictionaryRef progressParameter)

来判断是否已经匹配到了字体,第一个参数使我们的描述字体数组,第二个设为NULL,第三个参数为回调block。

block的state参数为当前匹配的状态,progressParmeter为进度参数,其中也包含错误信息。

我们所用到的state有下面这些:

kCTFontDescriptorMatchingDidBegin       //开始匹配

kCTFontDescriptorMatchingDidFinish      //匹配成功

kCTFontDescriptorMatchingWillBeginDownloading//字体开始下载

kCTFontDescriptorMatchingDidFinishDownloading//字体下载成功

kCTFontDescriptorMatchingDownloading    //下载中

kCTFontDescriptorMatchingDidFailWithError  //匹配失败

这些状态的回调顺序为

kCTFontDescriptorMatchingDidBegin

kCTFontDescriptorMatchingWillBeginDownloading

kCTFontDescriptorMatchingDownloading(多次调用)

kCTFontDescriptorMatchingDidFinishDownloading

kCTFontDescriptorMatchingDidFinish

其中kCTFontDescriptorMatchingDownloading会多次调用,来方便我们更新下载的进度条。

没接到一个回调状态我们就可以进行相应的UI处理,可以使用block,也可以使用通知。

progressParmeter我们用到了两个属性

因为它是CFDictionaryRef,所以首先我们应该先把它转为NSDictionary

当前下载进度的键为kCTFontDescriptorMatchingPercentage

错误对象的键为kCTFontDescriptorMatchingError

如果返回的状态是kCTFontDescriptorMatchingDidFailWithError,那我们就可以通过kCTFontDescriptorMatchingError来得到错误日志了。

IOS字体下载的更多相关文章

  1. iOS 字体下载

    iOS可以动态的为系统下载字体,这些字体都下载到了系统的目录下,并且可以被其他应用公用 来看下如何实现动态下载: // 创建下载字体请求描述的准备 NSMutableDictionary *attrs ...

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

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

  3. 一文让你彻底了解iOS字体相关知识

    写本文的契机主要是把自己整理的关于iOS字体方面的知识不断更新写在这篇博文中,用来自己以后查阅. 一.iOS原生字体展示 在label中选择字体的font,并把font由system改成custom后 ...

  4. 如何为ios酷我音乐盒下载导出的音乐文件(使用Java程序设计)

    这个工具已经准备第二版,读者了解编程软件,可以直接使用,请阅读和使用这个场地 http://blog.csdn.net/jzj1993/article/details/44459983 本文所涉及内容 ...

  5. iOS字体大小

    1,iOS 字体大小单位是pt——磅. 英文字体的1磅,相当于1/72 英寸,约等于1/2.8mm. px:相对长度单位.像素(Pixel).(PS字体) pt:绝对长度单位.点(Point).(iO ...

  6. iOS字体加载三种方式

    静态加载 动态加载 动态下载苹果提供的多种字体 其他 打印出当前所有可用的字体 检查某字体是否已经下载 这是一篇很简短的文章,介绍了 iOS 自定义字体加载的三种方式. 静态加载 这个可以说是最简单最 ...

  7. (转)iOS字体

    一.iOS原生字体展示 在 label中选择字体的font,并把font由system改成custom后,就能在family中看到72种特殊字体.这些里面就有很炫的字体,但 是全部是只针对英文数字,对 ...

  8. 仿IOS圆形下载进度条

    /** * Created by C058 on 2016/5/25. */ public class MyHoriztalProgressBar extends ProgressBar { priv ...

  9. iOS 字体设置

    使用无衬线字体 body {     font-family: "Helvetica Neue", Helvetica, STHeiTi, sans-serif; }  iOS 4 ...

随机推荐

  1. python字符串操作(连接、比较、格式化等)(转)

    字符串连接 方法一: Python代码 >>> str1 = 'hello' >>> str2 = 'world' >>> str1_2 = st ...

  2. How to Copy and Paste in the Ubuntu Gnome Terminal

    How to Copy: Select the content in terminal use your mouse , and then use Ctrl + Shift + C to copy t ...

  3. 架构设计:负载均衡层设计方案(6)——Nginx + Keepalived构建高可用的负载层

    1.概述 前两遍文章中,我们一直在说后文要介绍Nginx + Keepalived的搭建方式.这篇文章开始,我们就来兑现前文的承诺,后续的两篇文章我们将介绍Nginx + Keepalived和 LV ...

  4. uboot 网络不通问题解决一例1

    平台:Hi3531 PHY:RTL8211 现象:在uboot中执行ping命令的时候,总是超时. 过程: 使用uboot自带的phy操作命令mii读出的数据全是0xff.这里要介绍一下uboot中的 ...

  5. C#基础--属性 字段

    访问修饰符: private: 私有成员,在类的内部才可以访问 protected: 受保护的成员,该类内部和继承类的内部可以访问 public: 公共成员, 完全公开, 没有访问限制 interna ...

  6. 保持查询语法指示的联接顺序Option(Force order)

    Option(Force order) 今天和大家分享一下 SQL中强制执行联接顺序Option(Force Order) 一.SQL本身SQL引擎优化已经做的非常好了,但是也有默认的多表连接引擎效果 ...

  7. seajs 源码解读

    之前面试时老问一个问题seajs 是怎么加载js 文件的 在网上找一些资料,觉得这个写的不错就转载了,记录一下,也学习一下 seajs 源码解读 seajs 简单介绍 seajs是前端应用模块化开发的 ...

  8. 使用 mina 传输大字节数组

    转载自:http://seara520.blog.163.com/blog/static/16812769820103214817781/ 使用mina传输超过2k以上的数据时(采用tcp方式,如果是 ...

  9. 简单介绍AngularJs Filters

    网站链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/angular-filters/ Filter作用就是接收一个输入,通过某 ...

  10. VC++ UTF-8与GBK格式转换

    // 注释:多字节包括GBK和UTF-8 int GBK2UTF8(char *szGbk,char *szUtf8,int Len) { // 先将多字节GBK(CP_ACP或ANSI)转换成宽字符 ...