iOS下载使用系统字体
iOS下载使用系统字体
通用开发中一般使用系统默认的字体;
另外系统也提供了一些其他字体我们可以选择下载使用
1:在mac上打开 字体册 app 即可查找系统支持的字体,适用于ios上开发使用
从mac 字体册上,主要是寻找所要的字体的PostScript 以方便在ios上下载使用

2. 以下代码主要是从苹果官网下载的例子中摘录的,在此标记备忘
如上图:假如我们要下载字体的PostScript 为 :STBaoli-SC-Regular
先来判断该字体是否在系统中存在,如果存在使用,不存在则下载
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;
}
下载指定字体代码
// 创建下载字体请求描述的准备
NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil];
CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs);
NSMutableArray *descs = [NSMutableArray arrayWithCapacity:];
[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:.]; 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:.]; // Log the font URL in the console
CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)fontName, ., 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;
});
3. 完整示例工程
https://developer.apple.com/library/content/samplecode/DownloadFont/Introduction/Intro.html
iOS下载使用系统字体的更多相关文章
- 显示iOS所有系统字体
显示iOS所有系统字体 源码地址: https://github.com/YouXianMing/UI-Component-Collection 效果图: 便于你开发中寻找适合自己的字体, demo中 ...
- SSH客户端工具连接Linux(有的也可以连接Windows、mac、iOS等多系统平台)
要远程操作Linux的话还是得靠SSH工具,一般来说,Linux是打开了默认22端口的SSH的服务端,如果我们要远程它的话,就需要一个SSH客户. 我对一款好用的工具主要需要满足以下几点. (1)连接 ...
- Android 更换系统字体......
Android 更换系统字体...... 原文:http://vision-apps.blogspot.hk/2012/02/android-better-way-to-apply-custom-fo ...
- ios开发设置不同字体
最近项目开发中遇到需要设置指定字体的需求,研究了一下字体设置,最后附有我写的一个小demo,先来看一下效果: 开始上网搜了一下,普遍说到以下方法 for(NSString *fontfamilynam ...
- IOS开发中UIFont字体设置
我们在开发中很多时候要设置UIlabel,UIbutton,UItextfield的字体,这个时候我们就需要用到UIFont,下面简单介绍一下UIFont的用法,仅供参考. UIFont用于获取和设置 ...
- Linux 桌面玩家指南:04. Linux 桌面系统字体配置要略
特别说明:要在我的随笔后写评论的小伙伴们请注意了,我的博客开启了 MathJax 数学公式支持,MathJax 使用$标记数学公式的开始和结束.如果某条评论中出现了两个$,MathJax 会将两个$之 ...
- ios 点餐系统
这个程序的主要界面就是一个TabBarController.总共三个标签,第一个是所有的可点的菜,第二个是已点的菜,第三个是可以留言或者查看所有留言. 下面是第一个页面: 右上角的i按钮是添加新菜,每 ...
- IOS下载查看PDF文件(有下载进度)
IOS(object-c) 下载查看 PDF 其实还是蛮容易操作的.在下载前,首先要把 IOS 可以保存文件的目录给过一遍: IOS 文件保存目录 IOS 可以自定义写入的文件目录,是很有限的,只能是 ...
- 安卓app中嵌入一个H5页面,当手机系统设置字体变大时,如何使H5页面的字体不会随用户自己调整的系统字体变化而变化?
webview.getSettings().setTextZoom(100);WebView加上这个设置后,WebView里的字体就不会随系统字体大小设置发生变化了. https://segmentf ...
随机推荐
- MySQL 的 utf8_general_ci 和 utf8_unicode_ci 有什么区别,应如何选择?
一般的解说是 utf8_general_ci 速度较快,utf8_unicode_ci 准确性较好 … 但快在那里,又好在什么地方呢? 首先就其准确性说,这世界上有无数种文字,除了通用的英文使用我们较 ...
- Matlab代码备忘
1.Matlab写入文件 set(hp1,'xdata',bbb(1,:),'ydata',bbb(2,:),'zdata',bbb(3,:)); M=size(bbb,2); name=strca ...
- 20145320 《Java程序设计》第10周学习总结
20145320 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 计算机网络概述 网络编程的实质就是两个(或多个)设备(例如计算机)之间的数据传输. 按照计算机网络的定义 ...
- Mac下安装UPnP Inspector
由于工作中需要用到UPnP Inspector这个工具,而这个工具在windows下安装非常简单,在Mac下安装却很麻烦,在此记录安装流程. 这个工具依赖于两个其他的库:Coherence(一个DLN ...
- ASP.NET MVC controller 之间传JS值
在ASP.NET MVC中有东西叫TempData,它的类型是TempDataDictionary,它与ViewData以及ViewBag的不同之处在于 它的内部是使用session来保存信息的,可以 ...
- ubuntu15.04 安装搜狗输入法
首先:打开 系统设置->软件和更新,添加以下源, deb http://archive.ubuntukylin.com:10006/ubuntukylin trusty main 然后 sudo ...
- Extjs 3.4 复选框的,默认选中 ,禁用,(纯属于自己代码中需要,总结!)
var sm = new Ext.grid.CheckboxSelectionModel( { //一个特定的选择模型,它将渲染一列复选框,可以用来选择或反选多行数据. ...
- li ul 说明
复制代码代码如下: <div id="menu"> <ul> <li><a href="#">首页</ ...
- struts2笔记4
1.自定义struts拦截器 应用场景:如果用户登陆后可以访问action中的所有方法,如果用户没有登陆不允许访问action中的方法,并且提示“你没有操作权限” 1)两个页面,一个用户登陆user. ...
- C#网络爬虫
CronMaker is a utility which helps you to build cron expressions. CronMaker uses Quartz open source ...