iOS 字体下载

参考文章:原文🔗


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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS6之后的系统,苹果官方支持动态下载官方提供的中文字体。这样不仅可以避免版权问题,也可以减少项目的体积。下面就...
    翱翔的魁魁阅读 6,986评论 3 2
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,633评论 7 249
  • 入夜的草原,除了寥寥无几的帐篷透出点点光亮,所能看到的便是广阔无垠的天空,和夜空中那满天繁星。
    云上冷歌阅读 414评论 0 0
  • 我要走了 去一个未知的地方 在那里 没有震耳欲聋的喧嚣 没有尔虞我诈的面孔 没有不知所措的慌张 在那里 只有自然的...
    妮妮雅阅读 236评论 2 1
  • 今天晚上突然很想吃泡面,于是我就煮了一碗,吃的时候感觉泡面的第一口永远是最香的,可是当我给老爸打电话的时候,他和...
    DoubleTT阅读 300评论 0 2