使用 UIFontWDCustomLoader 载入自定义字体

简介:

UIFontWDCustomLoader

https://github.com/daktales/UIFontWDCustomLoader

You can use UIFontWDCustomLoader category to load any compatible font into your iOS projects at runtime without messing with plist, font unknown names or strange magic.

The only things you'll have to know are your font filenames and this library name.

You can also use this library to load new fonts after app installation.

还记得你之前怎么将一个字体载入到 iOS 中的吗?设置plist文件,找字体真实的名字等等,各种匹配不上,显示不出效果想杀人对吧.哥今天就给你带来了一个 UIFont 的类目文件,它可以在 iOS 运行的时候动态载入你想要的字体,知不知道字体名字没关系,哥的类目知道.

你唯一需要知道的就是,你拖到工程中字体的名字,以及哥这个类目的名字.

当然,如果你的应用已经安装了,但是,你还是能够在安装后读取下载的字体的.

Using font(使用字体)

#import "UIFont+WDCustomLoader.h"

One time setup (Explicit registration):(一次设定,明确的注册)

/* FONT COLLECTION FILE (TTC OR OTC) */

// Create an NSURL for your font file: 'Lao MN.ttc'
NSURL *laoFontURL = [[NSBundle mainBundle] URLForResource:@"Lao MN" withExtension:@"ttc"]];

// Do the registration.
NSArray *fontPostScriptNames = [UIFont registerFontFromURL:laoFontURL];

// If everything went ok, fontPostScriptNames will become @[@"LaoMN",@"LaoMN-Bold"] 
// and collection will be registered.
// (Note: On iOS < 7.0 you will get an empty array)

// Then, anywhere in your code, you can do
UIFont *laoFont = [UIFont fontWithName:@"LaoMN" size:18.0f];

or

/* SINGLE FONT FILE (TTF OR OTF) */

// Create an NSURL for your font file: 'Lato-Hairline.ttf'
NSURL *latoHairlineFontURL = [[NSBundle mainBundle] URLForResource:@"Lato-Hairline" withExtension:@"ttf"]];

// Do the registration.
NSArray *fontPostScriptNames = [UIFont registerFontFromURL:latoHairlineFontURL];

// If everything went ok, fontPostScriptNames will become @[@"Lato-Hairline"] 
// and collection will be registered.

// Then, anywhere in your code, you can do
UIFont *latoHairlineFont = [UIFont fontWithName:@"Lato-Hairline" size:18.0f];

// or
UIFont *latoHairlineFont = [UIFont customFontWithURL:latoHairlineFontURL size:18.0f];

// or (*deprecated*)
UIFont *myCustomFont = [UIFont customFontOfSize:18.0f withName:@"Lato-Hairline" withExtension:@"ttf"];

No setup (Implicit registration)(不用设置,使用时注册)

/* SINGLE FONT (TTF OR OTF) */

// Create an NSURL for your font file: 'Lato-Hairline.ttf'
NSURL *latoHairlineFontURL = [[NSBundle mainBundle] URLForResource:@"Lato-Hairline" withExtension:@"ttf"]];

// Then, anywhere in your code, you can do
UIFont *latoHairlineFont = [UIFont customFontWithURL:latoHairlineFontURL size:18.0f];

// or (*deprecated*)
UIFont *myCustomFont = [UIFont customFontOfSize:18.0f withName:@"Lato-Hairline" withExtension:@"ttf"];

NOTE: Font registration will be made on first [ UIFont customFont… ] method call.

注意:你在注册字体前,需要先调用[UIFont customFont...]方法.

Prerequisites(前提条件)

UIFontWDCustomLoader requires:

  • ARC
  • Deployment target greater or equal to iOS 4.1
  • CoreText Framework
  • 需要ARC
  • >= iOS 4.1
  • 需要引入CoreText框架

This library has been tested with: iOS 5, 6 and 7

在 iOS 5,6,7 上都测试过了亲.

 

附带本人的测试结果^_^

*用的时候直接取

*仅注册一次,以后可以直接根据名字使用

上述两种方式都得出了一样的结果哦,亲.

 

再来一个组合显示:

结果,效果拔群!!

目录
相关文章
|
前端开发 API
css:网页引入字体@font-face以及动态加载字体
css:网页引入字体@font-face以及动态加载字体
278 0
css:网页引入字体@font-face以及动态加载字体
|
3月前
wxPython4.0.4加载splash图片
wxPython4.0.4加载splash图片
20 0
|
8月前
|
存储 PHP 索引
PHPimagettftext字体文件ttc的使用方法
在PHP编程开发中,使用字体文件ttc来渲染文本是一项非常实用的技术。ttc文件是TrueType字体集合文件的缩写,它可以包含多个TrueType字体,因此在使用时需要进行一些特殊处理。下面,我们将为大家介绍PHPimagettftext字体文件ttc的使用方法,希望能够对大家有所帮助。
128 0
|
10月前
使用5.0.0版本的cssbox将html文件转为图片文件,并解决字体显示问题
使用5.0.0版本的cssbox将html文件转为图片文件,并解决字体显示问题
259 0
|
JavaScript
Vue3——压缩字体font-spider,完美解决字体压缩后会出现字体消失现象
压缩字体font-spider,完美解决字体压缩后会出现字体消失现象
351 0
使用位图字体工具BMFont从图片生成自定义字体
上一篇转了别人的一篇文章,讲了BMFont的基本用法。对BMFont比较陌生的同学请点击这里先去学习:http://blog.csdn.net/keshuiyun/article/details/9960589。
1496 0
Dialog是逻辑字体,实际绘制时会选择不同字体
Dialog是逻辑字体,实际绘制时会选择不同字体
193 0
|
Web App开发 缓存 前端开发
网页端字体加载优化
本文讲的是网页端字体加载优化,网络字体加载看起来也许非常复杂,但如果你使用本文的字体加载模式的话,这也并不是一件复杂的事情。你可以将这些模式组合起来,创建一个兼容所有浏览器的字体加载方式。
1886 0

热门文章

最新文章