iOS_字体下载

iOS6之后的系统,苹果官方支持动态下载官方提供的中文字体。这样不仅可以避免版权问题,也可以减少项目的体积。下面就简单的介绍字体库的动态下载。

获取字体的PostScript

MAC系统自带的字体集中,具体位置如下图。
字体PostScript位置

1、判断字体是否已存在

示例代码如下:

+ (BOOL)isFontDownloaded:(NSString *)postScriptName{
    UIFont *font = [UIFont fontWithName:postScriptName size:12.0];
    if (font && ([font.fontName compare:postScriptName] == NSOrderedSame || [font.familyName compare:postScriptName] == NSOrderedSame)) {
        return YES;
    }
    return NO;
}

2、开始下载字体

示例代码如下:

+ (void)downloadFontWithPostScriptName:(NSString *)postScriptName
                              Progress:(void (^)(double progressValue))progress
                               Success:(void (^)(void))success
                                  Fail:(void (^)(NSString *errorMessage))fail{
    if ([self isFontDownloaded:postScriptName]) {
        return;
    }
    NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:postScriptName,kCTFontNameAttribute, nil];
    CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrsDictionary);
    NSMutableArray *descriptorArray = [NSMutableArray array];
    [descriptorArray addObject:(__bridge id)descriptor];
    CFRelease(descriptor);
    __block BOOL errorDuringDownload = NO;
    CTFontDescriptorMatchFontDescriptorsWithProgressHandler((__bridge CFArrayRef)descriptorArray, NULL, ^bool(CTFontDescriptorMatchingState state, CFDictionaryRef  _Nonnull progressParameter) {
        double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];
        if (state == kCTFontDescriptorMatchingDidBegin) {
            NSLog(@"字体开始匹配");
        }else if (state == kCTFontDescriptorMatchingDidFinish){
            if (!errorDuringDownload) {
                NSLog(@"字体%@匹配完成",postScriptName);
            }
        }else if (state == kCTFontDescriptorMatchingWillBeginDownloading){
            NSLog(@"%@字体开始下载",postScriptName);
        }else if (state == kCTFontDescriptorMatchingDidFinishDownloading){
            dispatch_async(dispatch_get_main_queue(), ^{
                // 在此修改UI控件的字体
                success();
            });
        }else if (state == kCTFontDescriptorMatchingDownloading){
            progress(progressValue);
        }else if (state == kCTFontDescriptorMatchingDidFailWithError){
            NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];
            NSString *errorMesage = [NSString string];
            if (error != nil) {
                errorMesage = [error description];
            } else {
                errorMesage = @"ERROR MESSAGE IS NOT AVAILABLE!";
            }
            // 设置下载失败标志
            errorDuringDownload = YES;
            fail(errorMesage);
        }
        return (bool)YES;
    });

}

代码地址:
https://github.com/FlyingKuiKui/DownLoadFontFromSystem.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值