iOS中动态下载系统提供的多种中文字体

从iOS6开始,苹果开始支持动态下载官方提供的中文字体到系统中。使用动态下载中文字体的API可以动态的向iOS系统中添加字体,这些字体文件都是下载到系统的目录中,所以并不会造成应用体积的增加。不过,由于下载的时候需要使用的名字是PostScript名称,所以如果你真正要动态下载相应的字体的话,还需要使用Mac内自带的应用“字体册”来获得相应字体的PostScript名称。
相关API的介绍。
1、我们先判断字体是否已经被下载下来。
<span style="color:#993300;">UIFont* aFont = [UIFont fontWithName:fontName size:12.];
// If the font is already downloaded</span>
<span style="color:#993300;">
if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame))
    {
        // Go ahead and display the sample text.
<span style="white-space: pre;">	</span>NSUInteger sampleIndex = [_fontNames indexOfObject:fontName];

<span style="white-space: pre;">	</span>_fTextView.text = [_fontSamples objectAtIndex:sampleIndex];
<span style="white-space: pre;">	</span>_fTextView.font = [UIFont fontWithName:fontName size:24.];

<span style="white-space: pre;">	</span>return;</span>
    }
2、如果字体已经下载过了,则直接使用。否则我们需要先准备下载字体API需要的参数
<span style="color:#993300;">// 用字体的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);</span>
3、准备好上面的descs变量后,就可以进行字体的下载了。
<span style="color:#993300;">__blockBOOL errorDuringDownload = NO;

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

double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];

if (state == kCTFontDescriptorMatchingDidBegin)
       <span style="white-space:pre">	</span>{
               NSLog(@"Begin Matching");
        }
        else if (state == kCTFontDescriptorMatchingDidFinish)
        {
               NSLog(@"%@ downloaded", fontName);
  <span style="white-space:pre">	</span>}
        else if (state == kCTFontDescriptorMatchingWillBeginDownloading)
        {
               NSLog(@"Begin Downloading");
        }
        else if (state == kCTFontDescriptorMatchingDidFinishDownloading)
        {
               NSLog(@"Finish downloading");
        }
        else if (state == kCTFontDescriptorMatchingDownloading)
        {
               NSLog(@"Downloading %.0f%% complete", progressValue);
        }
        else if (state == kCTFontDescriptorMatchingDidFailWithError)
        {
               NSLog(@"Download error: %@", _errorMessage);
        }
         return (bool)YES;
});</span>
通常需要在下载字体完成后开始使用字体,一般是将相应的字体代码放到kCTFontDescriptorMatchingDidFinish那个条件中,可以像苹果官方网站示例代码一样,用GCD来修改UI的逻辑,也可以发Notification来通知相应的Controller.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值