/**

APP中加入文件字体,使自己打包文件字体比较麻烦,原因在于:

1.字体库文件一般比较大,对于一般的APP相当于体积翻倍了,得不偿失

2.中文字体通常都有版权,需要处理相应的版权问题

所以我们可以动态来下载中文字体到系统中

首先我们要知道苹果支持那些中文字体:打开 Mac内自带应用 -> Finder -> 应用程序 -> 字体册

找到所对应字体的PostScript名称

*/

//下面就直接上代码,首先封装好一个下载字体工具

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@interface FontManager : NSObject

+ (instancetype)sharedManager;

/**

*  动态检测下载对应的PostScriptName的字体,必须是苹果支持的才行

*

*  @param fontName 字体的PostScriptName

*  @param fontSize 字体大小

*  @param complete 完成时的回调

*  @param failure  失败时回调

*/

- (void)downloadFontWithPostScriptName:(NSString*)fontName fontSize:(CGFloat)fontSize complete:(void (^)(UIFont *font))complete failure:(void (^)(NSError *error))failure;

/**

*  动态检测下载对应的PostScriptName的字体,必须是苹果支持的才行

*

*  @param fontName 字体的PostScriptName

*  @param fontSize 字体大小

*  @param progress 下载进度

*  @param complete 完成时的回调

*  @param failure  失败时回调

*/

- (void)downloadFontWithPostScriptName:(NSString*)fontName fontSize:(CGFloat)fontSize progress:(void (^)(CGFloat progress))progress complete:(void (^)(UIFont *font))complete failure:(void (^)(NSError *error))failure;

@end

#import "FontManager.h"

#import <CoreText/CoreText.h>

@implementation FontManager

+ (instancetype)sharedManager

{

static FontManager *manager = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

manager = [[FontManager alloc]init];

});

return manager;

}

- (BOOL)isFontDownloaded:(NSString *)fontName

{

#warning 每次重新启动应用时,系统都会自动重新匹配字体,所以,就算下载过该字体,应用启动时该方法仍会返回NO

UIFont *aFont = [UIFont fontWithName:fontName size:12.0];

if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame)) {

return YES;

}

return NO;

}

- (void)downloadFontWithPostScriptName:(NSString*)fontName fontSize:(CGFloat)fontSize complete:(void (^)(UIFont *font))complete failure:(void (^)(NSError *error))failure

{

[self downloadFontWithPostScriptName:fontName fontSize:fontSize progress:nil complete:complete failure:failure];

}

- (void)downloadFontWithPostScriptName:(NSString*)fontName fontSize:(CGFloat)fontSize progress:(void (^)(CGFloat progress))progress complete:(void (^)(UIFont *font))complete failure:(void (^)(NSError *error))failure

{

if ([self isFontDownloaded:fontName]) {

NSLog(@"字体已下载");

dispatch_async(dispatch_get_main_queue(), ^{

UIFont *font = [UIFont fontWithName:fontName size:fontSize];

complete(font);

});

return;

}

// 用字体的PostScript名字创建一个Dictionary

NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName,kCTFontNameAttribute, nil];

// 创建一个字体描述对象CTFontDescriptorRef

CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs);

// 将字体描述对象放到一个NSMutableArray中

NSMutableArray *descs = [NSMutableArray array];

[descs addObject:(__bridge id)desc];

CFRelease(desc);

__block BOOL errorDuringDownload = NO;

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

CGFloat progressValue = [[(__bridge NSDictionary*)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] floatValue];

switch (state) {

case kCTFontDescriptorMatchingDidBegin:

NSLog(@"字体已经匹配");

break;

case kCTFontDescriptorMatchingDidFinish:

if (!errorDuringDownload) {

NSLog(@"字体%@ 下载完成",fontName);

//                    NSLog(@"%@",(__bridge NSDictionary*)progressParameter);

dispatch_async(dispatch_get_main_queue(), ^{

//更新UI

UIFont *font = [UIFont fontWithName:fontName size:fontSize];

complete(font);

});

}

break;

case kCTFontDescriptorMatchingWillBeginDownloading:

NSLog(@"字体开始下载");

break;

case kCTFontDescriptorMatchingDidFinishDownloading:

{

NSLog(@"字体下载完成");

dispatch_async(dispatch_get_main_queue(), ^{

//更新UI

UIFont *font = [UIFont fontWithName:fontName size:fontSize];

complete(font);

});

}

break;

case kCTFontDescriptorMatchingDownloading:

{

NSLog(@"下载进度 %.0f%%",progressValue);

dispatch_async(dispatch_get_main_queue(), ^{

if (progress) {

progress(progressValue);

}

});

}

break;

case kCTFontDescriptorMatchingDidFailWithError:

{

NSString *errorMessage = nil;

NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];

if (error != nil) {

errorMessage = [error description];

} else {

errorMessage = @"ERROR MESSAGE IS NOT AVAILABLE!";

}

// 设置标志

errorDuringDownload = YES;

NSLog(@"下载错误: %@", errorMessage);

dispatch_async(dispatch_get_main_queue(), ^{

if (failure) {

failure(error);

}

});

}

break;

default:

break;

}

return @YES;

});

}

@end

//开始使用

- (void)viewDidLoad {

[super viewDidLoad];

UILabel *fontLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 40)];

fontLabel.textAlignment = NSTextAlignmentCenter;

fontLabel.text = @"开始下载DFWaWaSC-W5字体";

fontLabel.font = [UIFont fontWithName:@"DFWaWaSC-W5" size:20];

[self.view addSubview:fontLabel];

[[FontManager sharedManager]downloadFontWithPostScriptName:@"DFWaWaSC-W5" fontSize:20 complete:^(UIFont *font) {

fontLabel.font = font;

} failure:^(NSError *error) {

}];

}

iOS 动态下载系统的中文字体相关推荐

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

    作者刘文涛 转载请注明出处 一.功能简介 1.前言 为了实现更好的字体效果,有些应用在自己的应用资源包中加入了字体文件.但自己打包字体文件比较麻烦,原因在于: 1.字体文件通常比较大,10~20M是常 ...

  2. UIFontDownLoad ----动态下载系统提供的字体

    程序运行结果如下 : 当点击对应单元格实现下载对应的字体. 控制台打印结果如下 : 2015-10-05 11:14:04.132 UIFontDownLoad[12721:86827] state ...

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

    一.场景.当前众多APP使用情况 在WWDC的内容公开之前,大家都以为iOS系统里面只有一种中文字体.为了达到更好的字体效果,有些应用在自己的应用资源包中加入了字体文件.但自己打包字体文件比较麻烦,原 ...

  4. ios动态下载多种中文字体

    引言 在今年 WWDC 的内容公开之前,大家都以为 iOS 系统里面只有一种中文字体.为了达到更好的字体效果,有些应用在自己的应用资源包中加入了字体文件.但自己打包字体文件比较麻烦,原因在于: 1.字 ...

  5. iOS 开发之动态下载系统提供的多种中文字体

    使用动态下载中文字体的API可以动态地向iOS系统中添加字体文件,这些字体文件都是下载到系统的目录中(目录是/private/var/mobile/Library/Assets/com_apple_M ...

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

    从iOS6开始,苹果开始支持动态下载官方提供的中文字体到系统中.使用苹果官方提供的中文字体,既可以避免版权问题,又可以节省应用体积.该方案适合对有较多需求的应用. 使用动态下载中文字体的API可以动态 ...

  7. 为Linux系统增加中文字体支持:解决显示问题的三种方法

    Linux 增加中文字体支持 在使用 Linux 操作系统的过程中,用户经常会遇到中文字体显示问题,导致一些界面上的文字不能正确显示.为了解决这个问题,我们可以通过以下方法来增加中文字体支持. 一.安 ...

  8. 下载免费的中文字体及生成jsPDF需要的js文件

    前言 jspdf插件用于生成pdf文件,功能非常强大,但是美中不足是没有中文字体,使用中文的话需要单独下载 引用官方文档的话: The 14 standard fonts in PDF are lim ...

  9. mac的python换字体_matplotlib在MAC系统下中文字体显示问题

    最近想把部分python数据分析的代码从win系统迁移到MAC上,有部分图片上涉及中文显示,迁移到MAC上warning: UserWarning: findfont: Font family [u' ...

最新文章

  1. DotNet关键知识点——WCF篇(六)
  2. jodd-servlet工具集锦
  3. Redis 命令--Redis集合(Set)
  4. OpenCV直方图计算Histogram Calculation
  5. NET快速信息化系统开发框架 V3.2 - “用户管理”主界面使用多表头展示、增加打印功能...
  6. Outlets 和Referencing Outlets的区别
  7. MySQL 锁与MVCC :数据库的锁、MVCC、当前读、快照读、锁算法、死锁
  8. 定时调度系列之Quartz.Net详解
  9. maven编译,控制台中文乱码解决方案
  10. JavaWeb 注解
  11. php正则判断是否为图片格式,JS 获取文件后缀,判断文件类型(比如是否为图片格式)...
  12. 青出于蓝而胜于蓝 — Vue.js对Angular.js的那些进步
  13. 一个算法模型搞定千万种场景,人工智能领域出现一匹黑马
  14. Python爬虫:最牛逼的 selenium爬取方式!
  15. 解析Unity3D中计算法线矩阵的函数
  16. 使用IDEA如何对Java项目进行打包
  17. 猪呀,羊呀,送到哪里去?
  18. 计算机二级word另存为pdf,计算机二级题库word操作步骤.pdf
  19. 互联网广告市场下沉:快手趣头条必有一战
  20. 「金融帝国实验室」(Capitalism Lab)现行官方正版游戏『销售政策指引』(2021.07.23-08.15)

热门文章

  1. 你的邮箱是靓号么?告诉你如何注册短位邮箱靓号!
  2. 《50强》企业访谈之安华金和:安全大势当前,唯快不破
  3. 2021~2022 学年第二学期《信息安全》考试试题(A 卷)
  4. 一、01【Java概述】之Java概述
  5. python多项式拟合_最小二乘法—多项式拟合非线性函数
  6. 如何在vue项目中系统的使用iconfont字体图标
  7. ESP8266开发之旅 应用篇⑥ 检测周边WiFi杀手
  8. jQuery中slideToggle()的详细使用方法和解释
  9. 分享怎么将cad转换成pdf的使用技巧
  10. 个推0代码数据可视化实操 | 基于Tableau的中国奥运数据探索