iOS 字体下载
iOS可以动态的为系统下载字体,这些字体都下载到了系统的目录下,并且可以被其他应用公用
来看下如何实现动态下载:
// 创建下载字体请求描述的准备
NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil];
CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs);
NSMutableArray *descs = [NSMutableArray arrayWithCapacity:0];
[descs addObject:(__bridge id)desc];
CFRelease(desc); //开始下载字体
__block BOOL errorDuringDownload = NO;
CTFontDescriptorMatchFontDescriptorsWithProgressHandler( (__bridge CFArrayRef)descs, NULL, ^(CTFontDescriptorMatchingState state, CFDictionaryRef progressParameter) { //NSLog( @"state %d - %@", state, progressParameter); double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue]; if (state == kCTFontDescriptorMatchingDidBegin) {
dispatch_async( dispatch_get_main_queue(), ^ {
// Show an activity indicator
[_fActivityIndicatorView startAnimating];
_fActivityIndicatorView.hidden = NO; // Show something in the text view to indicate that we are downloading
_fTextView.text= [NSString stringWithFormat:@"Downloading %@", fontName];
_fTextView.font = [UIFont systemFontOfSize:14.]; NSLog(@"开始匹配...");
});
} else if (state == kCTFontDescriptorMatchingDidFinish) {
dispatch_async( dispatch_get_main_queue(), ^ {
// Remove the activity indicator
[_fActivityIndicatorView stopAnimating];
_fActivityIndicatorView.hidden = YES; // Display the sample text for the newly downloaded font
NSUInteger sampleIndex = [_fontNames indexOfObject:fontName];
_fTextView.text = [_fontSamples objectAtIndex:sampleIndex];
_fTextView.font = [UIFont fontWithName:fontName size:24.]; // Log the font URL in the console
CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)fontName, 0., NULL);
CFStringRef fontURL = CTFontCopyAttribute(fontRef, kCTFontURLAttribute);
NSLog(@"%@", (__bridge NSURL*)(fontURL));
CFRelease(fontURL);
CFRelease(fontRef); if (!errorDuringDownload) {
NSLog(@"%@ downloaded", fontName);
}
});
} else if (state == kCTFontDescriptorMatchingWillBeginDownloading) {
dispatch_async( dispatch_get_main_queue(), ^ {
// Show a progress bar
_fProgressView.progress = 0.0;
_fProgressView.hidden = NO;
NSLog(@"开始下载...");
});
} else if (state == kCTFontDescriptorMatchingDidFinishDownloading) {
dispatch_async( dispatch_get_main_queue(), ^ {
// Remove the progress bar
_fProgressView.hidden = YES;
NSLog(@"下载完成");
});
} else if (state == kCTFontDescriptorMatchingDownloading) {
dispatch_async( dispatch_get_main_queue(), ^ {
// Use the progress bar to indicate the progress of the downloading
[_fProgressView setProgress:progressValue / 100.0 animated:YES];
NSLog(@"下载进度 %.0f%% complete", progressValue);
});
} else if (state == kCTFontDescriptorMatchingDidFailWithError) {
// An error has occurred.
// Get the error message
NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];
if (error != nil) {
_errorMessage = [error description];
} else {
_errorMessage = @"ERROR MESSAGE IS NOT AVAILABLE!";
}
// Set our flag
errorDuringDownload = YES; dispatch_async( dispatch_get_main_queue(), ^ {
_fProgressView.hidden = YES;
NSLog(@"下载失败: %@", _errorMessage);
});
} return (bool)YES;
});
NSString *ffontName = @"STBaoli-SC-Regular";
UIFont* aFont = [UIFont fontWithName:fontName size:.]; // 判断字体是否已经下载
if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame)) {
// 使用已下载的字体
NSUInteger sampleIndex = [_fontNames indexOfObject:fontName];
_fTextView.text = [_fontSamples objectAtIndex:sampleIndex];
_fTextView.font = [UIFont fontWithName:fontName size:.];
return;
}
iOS 字体下载的更多相关文章
- IOS字体下载
结合书本与苹果官方给的例子后,总结下下载的方法. 苹果给我们提供了很多漂亮的字体,只是有些字体设备并没有内置,需要我们去下载才行. 系统提供给我们的字体名我们可以通过mac系统提供的字体册来查阅. 得 ...
- iOS 动态下载系统提供的中文字体
使用系统提供的中文字体,既可避免版权问题,又可以减小应用体积 #pragma mark - 判断字体是否已经被下载 - (BOOL)isFontDownLoaded:(NSString *)fontN ...
- 一文让你彻底了解iOS字体相关知识
写本文的契机主要是把自己整理的关于iOS字体方面的知识不断更新写在这篇博文中,用来自己以后查阅. 一.iOS原生字体展示 在label中选择字体的font,并把font由system改成custom后 ...
- 如何为ios酷我音乐盒下载导出的音乐文件(使用Java程序设计)
这个工具已经准备第二版,读者了解编程软件,可以直接使用,请阅读和使用这个场地 http://blog.csdn.net/jzj1993/article/details/44459983 本文所涉及内容 ...
- iOS字体大小
1,iOS 字体大小单位是pt——磅. 英文字体的1磅,相当于1/72 英寸,约等于1/2.8mm. px:相对长度单位.像素(Pixel).(PS字体) pt:绝对长度单位.点(Point).(iO ...
- iOS字体加载三种方式
静态加载 动态加载 动态下载苹果提供的多种字体 其他 打印出当前所有可用的字体 检查某字体是否已经下载 这是一篇很简短的文章,介绍了 iOS 自定义字体加载的三种方式. 静态加载 这个可以说是最简单最 ...
- (转)iOS字体
一.iOS原生字体展示 在 label中选择字体的font,并把font由system改成custom后,就能在family中看到72种特殊字体.这些里面就有很炫的字体,但 是全部是只针对英文数字,对 ...
- 仿IOS圆形下载进度条
/** * Created by C058 on 2016/5/25. */ public class MyHoriztalProgressBar extends ProgressBar { priv ...
- iOS 字体设置
使用无衬线字体 body { font-family: "Helvetica Neue", Helvetica, STHeiTi, sans-serif; } iOS 4 ...
随机推荐
- Linux系统xinetd服务启动不了
Linux系统xinetd服务启动不了 xinetd服务时发现xinetd服务启动不了,并出现错误提示xinetd:unrecognized service,当出现这个错误提示的时候说明系统未安装xi ...
- centos6.5环境下的web项目mysql编码方式导致的中文乱码问题
最近在centos6.5下部署web项目时网页出现中文乱码的问题,在排除掉php之后,把问题锁定在mysql的编码方式上. 解决方法如下: 首先进入mysql命令行,输入命令:SHOW VARIABL ...
- 连接mysql报zone时区错误
报错信息: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zon ...
- 剑指offer字符串1
面试题5:替换空格 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. class Solu ...
- kubenetes 环境的塔建
最近听我朋友说他们公司准备上云,全线把服务迁到 k8s 上面,一下感觉,我们就 lower 了不少,之前服务器一直跑的就是 docker ,想想弄到 k8s 应该还是没有啥,于是我们也开始改造了 参考 ...
- 咸鱼入门到放弃6--jsp<一>三指令
JSP全称是Java Server Pages 它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术.JSP这门技术的最大的特点在于,写jsp就像在写html,但它相比htm ...
- PyCharm 怎么查看 Python 的变量类型和变量内容
一.在程序的某一行添加断点 二.选择 debug 程序
- Linux环境搭建 | 手把手教你如何安装CentOS7虚拟机
centos 下载地址: 可以去官网下载最新版本:https://www.centos.org/download/ 以下针对各个版本的ISO镜像文件,进行一一说明: CentOS-7.0-x86_64 ...
- LOJ.6160.[美团CodeM初赛 RoundA]二分图染色(容斥 组合)
题目链接 \(Description\) 求在\(2n\)个点的完全二分图(两边各有\(n\)个点)上确定两组匹配,使得两个匹配没有交集的方案数. \(n\leq10^7\). \(Solution\ ...
- JavaScript(六)
定时器 定时器在javascript中的作用1.制作动画2.异步操作3.函数缓冲与节流 定时器类型及语法 /* 定时器: setTimeout 只执行一次的定时器 clearTimeout 关闭只执行 ...