IOS字体下载

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

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

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

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

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

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

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

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

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

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

UIFont* aFont = [UIFont fontWithName:fontName size:12.];
    // 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:24.];
        return;
    }

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

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

 1     // Create a dictionary with the font's PostScript name.
 2     NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil];
 3     
 4     // Create a new font descriptor reference from the attributes dictionary.
 5     CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs);
 6     
 7     NSMutableArray *descs = [NSMutableArray arrayWithCapacity:0];
 8     [descs addObject:(__bridge id)desc];
 9     CFRelease(desc);
10     
11     __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来得到错误日志了。

原文地址:https://www.cnblogs.com/madpanda/p/4279141.html