iOS 字体下载

2017-05-12  本文已影响1331人  红袖吾狗

参考文章:原文🔗


iOS可以动态的为系统下载字体,这些字体都下载到了系统的/private/var/mobile/Library/Assets/com_apple_MobileAsset_Font/目录下,并且可以被其他应用公用。当然,问题是应用的大小倒是没变,而你的手机空间在不知不觉中就变小了。\-.-/~

来看下如何实现动态下载:

CTFontDescriptorRef descRef = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)@{(NSString *)kCTFontNameAttribute: fontName});
    NSArray *descs = @[(__bridge id)descRef];
    
    CTFontDescriptorMatchFontDescriptorsWithProgressHandler((CFArrayRef)descs, NULL, ^bool(CTFontDescriptorMatchingState state, CFDictionaryRef  _Nonnull progressParameter) {
        
        if (state == kCTFontDescriptorMatchingWillBeginDownloading) {
            NSLog(@"开始下载");
        } else if (state == kCTFontDescriptorMatchingDownloading) {
            double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];
            NSLog(@"下载进度:%0.2lf",progressValue);
        } else if (state == kCTFontDescriptorMatchingDidFinishDownloading) {
            NSLog(@"下载完成");
            
            dispatch_async(dispatch_get_main_queue(), ^{
                self.label.font = [UIFont fontWithName:fontName size:12];
            });
        } else if (state == kCTFontDescriptorMatchingDidBegin) {
            NSLog(@"开始");
        }
        
        return YES;
    });
改变前.png 改变后.png
上一篇下一篇

猜你喜欢

热点阅读