iOS下载使用系统字体

通用开发中一般使用系统默认的字体;

另外系统也提供了一些其他字体我们可以选择下载使用

1:在mac上打开 字体册 app 即可查找系统支持的字体,适用于ios上开发使用

从mac 字体册上,主要是寻找所要的字体的PostScript 以方便在ios上下载使用

2. 以下代码主要是从苹果官网下载的例子中摘录的,在此标记备忘

如上图:假如我们要下载字体的PostScript 为 :STBaoli-SC-Regular

先来判断该字体是否在系统中存在,如果存在使用,不存在则下载

    NSString *ffontName = @"STBaoli-SC-Regular";UIFont* aFont = [UIFont fontWithName:fontName size:12.];// 判断字体是否已经下载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:24.];return;}

View Code

下载指定字体代码

    // 创建下载字体请求描述的准备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 fontNSUInteger sampleIndex = [_fontNames indexOfObject:fontName];_fTextView.text = [_fontSamples objectAtIndex:sampleIndex];_fTextView.font = [UIFont fontWithName:fontName size:24.];// Log the font URL in the consoleCTFontRef 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 messageNSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];if (error != nil) {_errorMessage = [error description];} else {_errorMessage = @"ERROR MESSAGE IS NOT AVAILABLE!";}// Set our flagerrorDuringDownload = YES;dispatch_async( dispatch_get_main_queue(), ^ {_fProgressView.hidden = YES;NSLog(@"下载失败: %@", _errorMessage);});}return (bool)YES;});

View Code

3. 完整示例工程

https://developer.apple.com/library/content/samplecode/DownloadFont/Introduction/Intro.html

iOS下载使用系统字体相关推荐

  1. iOS 查看所有系统字体

    一,查看系统字体的代码: NSArray * fontFamilyArray = [UIFont familyNames];for (NSString* familyName in fontFamil ...

  2. ios、android 系统字体说明

    后来了解到的手机系统 ios.android 等是不支持微软雅黑字体,为了满足产品的需要,保证视觉稿的还原度,手机端是如何定义微软雅黑字体呢? 相信大家会想到 @font-face 定义为微软雅黑字体 ...

  3. iOS 定制应用程序字体

    使用Quartz Core绘制文字非常简单,苹果的Quartz 2D参考中演示了如何使用CGContextShowTextAtPoint函数绘制文本.不幸的是,这个函数不支持Unicode字符的绘制( ...

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

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

  5. html css ios系统字体,让iOS在CSS中选择系统字体Helvetica Neue或San Francisco

    on iOS and OS X by using the "-apple-system" CSS value for the "font-family" CSS ...

  6. iOS 8全系统苹果盘古完美越狱工具1.0.0版For Windows下载地址

    iOS 8全系统苹果盘古完美越狱工具1.0.0版For Windows下载地址iOS 8全系统苹果盘古完美越狱工具1.0.0版For Windows下载地址iOS 8全系统苹果盘古完美越狱工具1.0. ...

  7. iOS系统字体如何使用

    1,纯代码使用系统字体 如苹方字体,DIN等等: 使用方法如下: _addressL.font = [UIFont fontWithName:@"PingFangSC-Regular&quo ...

  8. android 设置ios 字体大小设置,解决修改系统字体大小APP字体跟着变大的问题

    前言 最近在做项目的时候,碰到了这个问题,具体是:app中字体大小用了sp,然后修改手机系统字体大小后,sp设置的文字大小跟着系统变了,导致了布局挤压以及一系列的问题,经过研究找到了解决方法,在此记录 ...

  9. 硬件断点反跳似乎_高性能应用程序:多路复用,反跳,系统字体和其他技巧

    硬件断点反跳似乎 by Atila Fassina 通过阿蒂拉·法西纳(Atila Fassina) 高性能应用程序:多路复用,反跳,系统字体和其他技巧 (High Performance Apps: ...

最新文章

  1. 没答好「进程间通信」,被面试官挂了....
  2. LeetCode - 413. Arithmetic Slices - 含中文题意解释 - O(n) - ( C++ ) - 解题报告
  3. RHEL 5.4 安装Oracle 11gR2, 安装篇2
  4. 固特异发布人工智能球形轮胎:自动变形、自我修复、超级灵活
  5. Day 20:网络编程(1)
  6. 因触屏故障 美国监管部门对15.9万辆特斯拉进行调查
  7. android alpha java_Android support 26.0.0-alpha1 产生的问题(zz)
  8. android的listview+BaseAdapter的例子
  9. Pytorch深度学习实战项目回顾
  10. 前景检测算法(八)--SACON算法
  11. pythontiff怎么设置_在python中使用tiff文件
  12. K均值聚类算法(K-Means)
  13. 涨知识丨一看就懂的遥感数据科普,附5个数据集下载资源
  14. 路由器 三层交换机 网关有什么区别
  15. SpaceDesk PC版+安卓版(安装包下载)
  16. 数据库函数依赖与候选码求解
  17. 计算机中心对临床质量考核标准,三级公立医院绩效考核第13项指标室间质量评价的解读...
  18. 手机上竟然也能用 Win10 ?一模一样啊!
  19. Navicat 8.0 for MySQL的注册码
  20. AUTOCAD2017

热门文章

  1. java *** 制作圣诞贺卡
  2. 和差化积公式 与 积化和差公式的推导
  3. 投入产出综合平衡分析
  4. 图解http协议 部分笔记
  5. python编写一元二次方程求根公式复数,VB:一元二次方程算法中求根公式如何做复数处理?...
  6. MQ 系列之 ActiveMQ 介绍
  7. jquery中获取复选框的值
  8. 欧美市场独立站选品技巧
  9. java基础-静态代理与动态代理
  10. Vue路由与无痕浏览(replace)