iOS 动态下载系统提供的中文字体
使用系统提供的中文字体,既可避免版权问题,又可以减小应用体积
#pragma mark - 判断字体是否已经被下载
- (BOOL)isFontDownLoaded:(NSString *)fontName{
UIFont *font = [UIFont fontWithName:fontName size:12.0];
if (font && [font.fontName compare:fontName] == NSOrderedSame) {
return YES;
}
else{
return NO;
}
} #pragma mark - - (void)downLoadFont{ //如果字体已经被下载过了,则可以直接使用,否则我们需要先准备下载字体API需要的一些参数
NSString *fontName = @"FZLTTHK-GBK1.0"; //用自体的名字创建一个Dictionary
NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName,kCTFontNameAttribute, nil]; //创建一个字体描述对象CTFontDescriptorRef
CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs); //将字体描述对象放到一个NSMutableArray中
NSMutableArray *descs = [NSMutableArray arrayWithCapacity:];
[descs addObject:(__bridge id)desc];
CFRelease(desc); //字体下载
__block BOOL errorDuringDownLoad = NO; CTFontDescriptorMatchFontDescriptorsWithProgressHandler((__bridge CFArrayRef)descs, NULL, ^bool(CTFontDescriptorMatchingState state, CFDictionaryRef _Nonnull progressParameter) { double progress = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue]; switch (state) {
case kCTFontDescriptorMatchingDidBegin:
{
NSLog(@"字体已经匹配");
}
break;
case kCTFontDescriptorMatchingDidFinish:
{
if (!errorDuringDownLoad) { NSLog(@"字体:%@下载完成",fontName);
}
}
break;
case kCTFontDescriptorMatchingWillBeginQuerying:
{ }
break;
case kCTFontDescriptorMatchingStalled:
{ }
break;
case kCTFontDescriptorMatchingWillBeginDownloading:
{
NSLog(@"字体开始下载");
}
case kCTFontDescriptorMatchingDownloading:
{
NSLog(@"下载进度:%2f%%",progress);
}
break;
case kCTFontDescriptorMatchingDidFinishDownloading:
{
NSLog(@"字体下载完成");
dispatch_async(dispatch_get_main_queue(), ^{ //可以在这里修改UI控件的字体
});
}
break;
case kCTFontDescriptorMatchingDidMatch:
{ }
break;
case kCTFontDescriptorMatchingDidFailWithError:
{ errorDuringDownLoad = YES; NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];
if (error == nil) { NSLog(@"%@",[error description]);
}
else{
NSLog(@"ERROR MESSAGE IS NOT AVAILABLE!");
}
}
break;
default:
break;
} return YES;
}); }
在下载完成后开始使用字体,一般将使用字体的代码放在 kCTFontDescriptorMatchingDidFinishDownloading 这个判断条件中。
字体下载完成后修改UI有两种方式:
1.用GCD来修改
2.可以发送通知来通知相应的Controller
iOS 动态下载系统提供的中文字体的更多相关文章
- UIFontDownLoad ----动态下载系统提供的字体
程序运行结果如下 : 当点击对应单元格实现下载对应的字体. 控制台打印结果如下 : 2015-10-05 11:14:04.132 UIFontDownLoad[12721:86827] state ...
- 【读书笔记】iOS-UIFont-动态下载系统提供的多种中文字体网址
苹果可使用的字体列表: https://support.apple.com/zh-cn/HT202599 动态下载字体的代码demo: https://developer.apple.com/libr ...
- 【读书笔记】iOS-UIFont-动态下载系统提供的字体-官方代码
一,工程目录 二,AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOption ...
- centos 系统字体库安装中文字体
一,centos系统默认不支持中文字体的,需要手动安装windows系统中的中文字体库到centos中. 首先,将windows系统中的字体拷贝出来: windows:打开C:\Windows\Fon ...
- Arch-Linux-添加中文字体
代码下载: Arch-Linux-添加中文字体.txt
- [Xcode 实际操作]九、实用进阶-(2)遍历设备(输出系统)上的所有字体
目录:[Swift]Xcode实际操作 在实际工作中,经常需要调整界面元素的字体种类. 本文将演示输出系统提供的所有字体,方便检索和使用. 在项目导航区,打开视图控制器的代码文件[ViewContro ...
- 解决CentOS无法显示中文字体 | 系统运维 | Web2.0
解决CentOS无法显示中文字体 | 系统运维 | Web2.0 About Me 博客园 devops 前端 张家港水蜜桃 傍晚好! 2013年09月12日 17:56:08 ...
- iOS系统提供开发环境下命令行编译工具:xcodebuild
iOS系统提供开发环境下命令行编译工具:xcodebuild[3] xcodebuild 在介绍xcodebuild之前,需要先弄清楚一些在XCode环境下的一些概念[4]: Workspace:简单 ...
- ubuntu14.04恢复系统默认中文字体
今天 Ubuntu14.04已发布就进行了更新,在配置过程中,无意安装了某些中文字体,导致系统的中文字体极其难看,根据网上说的修改配置文件和tweak 修改的方法都不能解决,最终找到的解决办法(htt ...
随机推荐
- 用HTML做的简单的个人简历
<html> <head> <title>table表格</title> <style type="text/css"> ...
- JQuery随笔
web请求中,外部样式文件的加载不影响页面,js的加载会影响页面的加载速度 window.onlond 页面所有元素加载完后执行包含js,css等加载完毕 $(function(){}) 页面中DOM ...
- java web学习之表单
前台页面与后台页面的数据又form表单完成. <form name ="form1" method="post" action="index ...
- centos6环境下安装tmux
Install tmux on CentOS 6:1. sudo rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-releas ...
- JS日期类型处理
Date 操作日期和时间的对象 Date.getDate( ) 返回一个月中的某一天 Date.getDay( ) 返回一周中的某一天 Date.getFullYear( ) 返回Date对象的年份字 ...
- map的四种遍历方式
map是Java中非常常用的一种数据结构,但map不同于set和list都继承自Collection接口. 所以map没有实现Collection的Iterator 方法,自身没有迭代器来遍历元素. ...
- Graphics2D字符串根据文本框缩小字体自动换行
/** * *描述: 长字符串缩小字体自动换行 *@param g *@param text 字符串 *@param lineWidth 单元格宽度 *@param cellHeight 单元格高度 ...
- php常见的关键字
一 instanceof 检测一个对象是否属于某个类型 <?php class A { } $a = new A(); if($a instanceof A) { echo "yes& ...
- python模块httplib的使用
GET: #lianxi-httplib.HTTPConnection.request-get.py import httplib class HttpRequestGETTest(object): ...
- VS2012 调试时 局部变量显示不全的问题解决
在工程上右键,打开属性页,配置属性——C/C++——优化,将优化改为“已禁用/Od“