程序运行结果如下 :

当点击对应单元格实现下载对应的字体.

控制台打印结果如下 :

2015-10-05 11:14:04.132 UIFontDownLoad[12721:86827] state 0 - {
}
2015-10-05 11:14:04.134 UIFontDownLoad[12721:86827] state 7 - {
    CTFontDescriptorMatchingResult =     (
        "UICTFontDescriptor <0x7fddd05388d0> = {\n    NSFontNameAttribute = Helvetica;\n    NSFontSizeAttribute = 12;\n}"
    );
    CTFontDescriptorMatchingSourceDescriptor = "UICTFontDescriptor <0x7fddd0797a80> = {\n    NSFont = \"STXingkai-SC-Light\";\n}";
}
2015-10-05 11:14:04.134 UIFontDownLoad[12721:86827] state 1 - {
    CTFontDescriptorMatchingResult =     (
        "UICTFontDescriptor <0x7fddd05388d0> = {\n    NSFontNameAttribute = Helvetica;\n    NSFontSizeAttribute = 12;\n}"
    );
}
2015-10-05 11:14:04.147 UIFontDownLoad[12721:85405] 字体已经匹配
2015-10-05 11:14:04.147 UIFontDownLoad[12721:85405] 字体下载完成
2015-10-05 11:14:05.562 UIFontDownLoad[12721:85405] STXingkai-SC-Light downloaded

程序实现的代码如下 :

//
//  ViewController.m
//  UIFontDownLoad
//
//  Created by mac1 on 15/10/5.
//  Copyright (c) 2015年 www.iphonetrain.com. All rights reserved.
//

#import "ViewController.h"

/*
 一种能够对文本格式和文本布局进行精细控制的文本引擎
 */
#import <CoreText/CoreText.h>

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)NSArray *fontNames;
@property (nonatomic,strong)NSArray *fontSamples;
@property (nonatomic,strong)UITableView *myTableView;

@end

static NSString *identify = @"cell";

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
 
    self.view.backgroundColor = [UIColor greenColor];

[self addData];
    [self creatUI];
    
    
    
}

//初始化界面
- (void)creatUI
{
    _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    [self.view addSubview:_myTableView];
    
    
    /*
     注册单元格
    [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identify];
     */
    
     
}

//初始化数据

- (void)addData
{
    _fontNames = [[NSArray alloc] initWithObjects:
                 @"STXingkai-SC-Light",
                 @"DFWaWaSC-W5",
                 @"FZLTXHK--GBK1-0",
                 @"STLibian-SC-Regular",
                 @"LiHeiPro",
                 @"HiraginoSansGB-W3",
                 nil];
    _fontSamples = [[NSArray alloc] initWithObjects:
                   @"汉体书写信息技术标准相",
                   @"容档案下载使用界面简单",
                   @"支援服务升级资讯专业制",
                   @"作创意空间快速无线上网",
                   @"兙兛兞兝兡兣嗧瓩糎",
                   @"㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩",
                   nil];

}

//行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _fontNames.count;
    
}

//创建单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

//直接显示内容就不注册单元格

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        
    }
    cell.textLabel.text = _fontNames[indexPath.row];

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self asynchronouslySetFontName:_fontNames[indexPath.row]];
}

#pragma -mark -functions
//字体开始进行下载

- (void)asynchronouslySetFontName:(NSString *)fontName
{
    UIFont *aFont = [UIFont fontWithName:fontName size:12];
    
    //判断字体是否已经被下载
    if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame))
    {
        NSLog(@"字体已经被下载");
        return;
    }

//用字体的PostScript名字创建一个Dictionary
    NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName,kCTFontAttributeName, nil];
    
    
    // 创建一个字体描述对象CTFontDescriptorRef
    CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs);
    
    //将字体描述对象放到一个NSMutableArray中
    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(), ^ {
                NSLog(@"字体已经匹配");
            });
        } else if (state == kCTFontDescriptorMatchingDidFinish) {
            dispatch_async( dispatch_get_main_queue(), ^ {
                NSLog(@"字体下载完成");
                
                // Log the font URL in the console
                CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)fontName, 0., NULL);
                CFStringRef fontURL = CTFontCopyAttribute(fontRef, kCTFontURLAttribute);
                CFRelease(fontURL);
                CFRelease(fontRef);
                
                if (!errorDuringDownload) {
                    NSLog(@"%@ downloaded", fontName);
                }
            });
        } else if (state == kCTFontDescriptorMatchingWillBeginDownloading) {
            dispatch_async( dispatch_get_main_queue(), ^ {
                NSLog(@"字体开始下载");
            });
        } else if (state == kCTFontDescriptorMatchingDidFinishDownloading) {
            dispatch_async( dispatch_get_main_queue(), ^ {
                NSLog(@"字体下载完成");
            });
        } else if (state == kCTFontDescriptorMatchingDownloading) {
            dispatch_async( dispatch_get_main_queue(), ^ {
                NSLog(@"下载进度");
            });
        } else if (state == kCTFontDescriptorMatchingDidFailWithError) {
            NSLog(@"下载失败");
            
            NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];
            if (error != nil) {
                NSLog(@"errorMessage--%@-",[error description]);
            } else {
                NSLog(@"error message is not available");
            }
            errorDuringDownload = YES;
            dispatch_async( dispatch_get_main_queue(), ^ {
                NSLog(@"Download error: %@", [error description]);
            });
        }
        
        return (bool)YES;
    });
     
}

UIFontDownLoad ----动态下载系统提供的字体的更多相关文章

  1. iOS 动态下载系统提供的中文字体

    使用系统提供的中文字体,既可避免版权问题,又可以减小应用体积 #pragma mark - 判断字体是否已经被下载 - (BOOL)isFontDownLoaded:(NSString *)fontN ...

  2. 【读书笔记】iOS-UIFont-动态下载系统提供的字体-官方代码

    一,工程目录 二,AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOption ...

  3. 【读书笔记】iOS-UIFont-动态下载系统提供的多种中文字体网址

    苹果可使用的字体列表: https://support.apple.com/zh-cn/HT202599 动态下载字体的代码demo: https://developer.apple.com/libr ...

  4. OSGI(面向Java的动态模型系统)

    基本简介编辑 OSGI服务平台提供在多种网络设备上无需重启的动态改变构造的功能.为了最小化耦合度和促使这些耦合度可管理,OSGi技术提供一种面向服务的架构,它能使这些组件动态地发现对方.OSGi联 O ...

  5. OSGi 系列(一)之什么是 OSGi :Java 语言的动态模块系统

    OSGi 系列(一)之什么是 OSGi :Java 语言的动态模块系统 OSGi 的核心:模块化.动态.基于 OSGi 就可以模块化的开发 java 应用,模块化的部署 java 应用,还可以动态管理 ...

  6. OSGI 面向Java的动态模型系统

    OSGI (面向Java的动态模型系统) OSGi(Open Service Gateway Initiative)技术是Java动态化模块化系统的一系列规范.OSGi一方面指维护OSGi规范的OSG ...

  7. Android该系统提供的服务--Vibrator(振子)

    Android该系统提供的服务--Vibrator(振子) --转载请注明出处:coder-pig Vibrator简单介绍与相关方法: watermark/2/text/aHR0cDovL2Jsb2 ...

  8. (原)SQL Server 系统提供功能的三个疑惑

    本文目录列表: 1.SQL Server系统提供的部分疑惑概述2.系统函数调用时DEFAULT代替可选参数使用不统一3.队列字段列message_enqueue_time记录的是UTC日期时间 4.@ ...

  9. 很简单的在Ubuntu系统下安装字体和切换默认字体的方法

    摘要: Ubuntu系统安装好后,默认字体对于中文的支持看上去不太美丽,于是很多朋友可能需要设置系统的默认字体为自己喜欢的字体.本文主要介绍如何解决这两个问题. 说明:测试系统是Ubuntu14.04 ...

随机推荐

  1. 在virtualenv中使用不同的Python版本

    来源:百度SEO公司   我有一个目前使用python 2.5.4运行的Debian系统. 我正确安装了virtualenv,一切正常. 我是否可以将virtualenv与其他版本的Python一起使 ...

  2. 使用jQuery快速高效制作网页交互特效-----JavaScript操作DOM对象

    一.DOM操作分类 使用JavaScript操作DOM时通常分为三类:DOM    Core.HTMl--DOM和CSS-DOM 二.访问节点 节点属性 三.节点信息 四.操作节点的属性 语法: ge ...

  3. spark读文件写入mysql(scala版本)

    package com.zjlantone.hive import java.util.Properties import com.zjlantone.hive.SparkOperaterHive.s ...

  4. Python学习之--基础语法

    一.定义 Python 是一种解释型.面向对象.动态数据类型的高级程序设计语言. 二.Python变量的命名规则 1. 变量名只能包含字母.数字和下划线: 2. 变量名不能包含空格: 3. 不要将Py ...

  5. ROS常用工具

    先将这个package放到工作空间的 src 目录中, 链接:点击打开链接 密码:u98p 然后在工作空间中加入依赖, rosdep install --from-paths src --ignore ...

  6. nodeJs 初学案例摘要

    在学习nodeJs的时候,照着文档做的,但是到最后的上传文件显示图片总是报错, 所用的fs.renameSync出错:Error: EXDEV, cross-device link not permi ...

  7. angularJs 中controller与sever

    网上找到的一个例子,感觉对于初学者理解将controller抽成服务有帮助.主要是方便理解什么时候应该来做服务. html部分 <!DOCTYPE html> <html ng-ap ...

  8. Go开发环境安装与环境变量配置

    1.Go安装包下载 https://studygolang.com/dl 2.安装 Windows平台下,直接下一步即可完成安装. 3.配置环境变量 系统变量 安装完成后,在系统变量的Path一栏,会 ...

  9. gradle的简单使用

    Gradle是一个基于JVM的构建工具,是一款通用灵活的构建工具,支持maven, Ivy仓库,支持传递性依赖管理,而不需要远程仓库或者是pom.xml和ivy.xml配置文件,基于Groovy,bu ...

  10. 跑shell脚本出错问题以及解决。

    脚本信息: #!/bin/bash function hello(){ cat <<YJT echo "hello world!!!" YJT } hello 错误: ...