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下载使用系统字体的更多相关文章

  1. 显示iOS所有系统字体

    显示iOS所有系统字体 源码地址: https://github.com/YouXianMing/UI-Component-Collection 效果图: 便于你开发中寻找适合自己的字体, demo中 ...

  2. SSH客户端工具连接Linux(有的也可以连接Windows、mac、iOS等多系统平台)

    要远程操作Linux的话还是得靠SSH工具,一般来说,Linux是打开了默认22端口的SSH的服务端,如果我们要远程它的话,就需要一个SSH客户. 我对一款好用的工具主要需要满足以下几点. (1)连接 ...

  3. Android 更换系统字体......

    Android 更换系统字体...... 原文:http://vision-apps.blogspot.hk/2012/02/android-better-way-to-apply-custom-fo ...

  4. ios开发设置不同字体

    最近项目开发中遇到需要设置指定字体的需求,研究了一下字体设置,最后附有我写的一个小demo,先来看一下效果: 开始上网搜了一下,普遍说到以下方法 for(NSString *fontfamilynam ...

  5. IOS开发中UIFont字体设置

    我们在开发中很多时候要设置UIlabel,UIbutton,UItextfield的字体,这个时候我们就需要用到UIFont,下面简单介绍一下UIFont的用法,仅供参考. UIFont用于获取和设置 ...

  6. Linux 桌面玩家指南:04. Linux 桌面系统字体配置要略

    特别说明:要在我的随笔后写评论的小伙伴们请注意了,我的博客开启了 MathJax 数学公式支持,MathJax 使用$标记数学公式的开始和结束.如果某条评论中出现了两个$,MathJax 会将两个$之 ...

  7. ios 点餐系统

    这个程序的主要界面就是一个TabBarController.总共三个标签,第一个是所有的可点的菜,第二个是已点的菜,第三个是可以留言或者查看所有留言. 下面是第一个页面: 右上角的i按钮是添加新菜,每 ...

  8. IOS下载查看PDF文件(有下载进度)

    IOS(object-c) 下载查看 PDF 其实还是蛮容易操作的.在下载前,首先要把 IOS 可以保存文件的目录给过一遍: IOS 文件保存目录 IOS 可以自定义写入的文件目录,是很有限的,只能是 ...

  9. 安卓app中嵌入一个H5页面,当手机系统设置字体变大时,如何使H5页面的字体不会随用户自己调整的系统字体变化而变化?

    webview.getSettings().setTextZoom(100);WebView加上这个设置后,WebView里的字体就不会随系统字体大小设置发生变化了. https://segmentf ...

随机推荐

  1. angularjs backbone 集成requirejs 模块化

    首先认识requirejs requirejs是个包加载器,核心功能是模块化管理,可以实现按需加载. 重点是明白 模块化不是按需加载. 模块化的意义: 是通过代码逻辑表明模块之间的依赖关系和执行顺序, ...

  2. ls 只显示目录

    只显示目录: ls -d */ 在实际应用中,我们有时需要仅列出目录,下面是 4 种不同的方法. 1. 利用 ls 命令的 -d 选项: $ ls -d */ 2. 利用 ls 命令的 -F 选项: ...

  3. LeetCode Burst Balloons

    原题链接在这里:https://leetcode.com/problems/burst-balloons/ 题目: Given n balloons, indexed from 0 to n-1. E ...

  4. Fiddler- -Composer创建和发送HTTP Request

    Fiddler的功能还有很多, 很多功能都没有被挖掘出来.这次我们介绍Fiddler中的一个非常有用的功能Composer,是用来创建和发送HTTP Request的.Composer的使用方法很简单 ...

  5. java 8增强的包装类

    ---恢复内容开始--- 1java为8个类型提供了对应的包装类.因为这8个类型并不是类或者对象,他们原本是不应该存在在java里的(这里说的只是原本),这里要强调的是,java允许把基本类型的值直接 ...

  6. [eclipse] Server at localhost was unable to start within 45 seconds.

    When debuging in the eclipse with Tomcat, i meet these error: Server Tomcat v7.0 Server at localhost ...

  7. ilbc编解码

    针对国内的博客或者技术论坛对 ILBC的论述都是把文章抄来抄去, 本人在此对 ILBC的具体代码实现详细列出代码. ILBC是由Global IP Sound公司提出的一种专为包交换网络通信设计的编解 ...

  8. 机器学习实战-K-nearest neighbors 算法的优缺点

    K临近算法是基于实例的学习,使用算法的时候我们必须要有接近分类结果的实例训练样本数据. 优点:精度高,对异常值不敏感 缺点: 时间复杂度和空间复杂度比较大.(如果训练样本数据集比较大,需要大量的空间来 ...

  9. Linux之curl命令详解

    url命令是一个功能强大的网络工具,它能够通过http.ftp等方式下载文件,也能够上传文件.其实curl远不止前面所说的那些功能,大家可以通过man curl阅读手册页获取更多的信息.类似的工具还有 ...

  10. Oracle logmnr工具分析日志脚本

    在oracle:对logmnr工具分析日志这个不陌生:对数据恢复应用场景经常用到:这里为了方便对归档日志进行分析: 当然网上有一个logmnr界面的工具:感觉很人性化:下载需要积分:为此写一个脚本. ...