使用 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 上都测试过了亲.

 

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

*用的时候直接取

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

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

 

再来一个组合显示:

结果,效果拔群!!

目录
相关文章
|
1月前
|
前端开发 Ubuntu Linux
CSS 系统字体堆栈参考
CSS 系统字体堆栈参考
25 0
|
2月前
|
前端开发
css实用技巧——异步加载图片时,在图片完成加载前,鼠标悬浮到占位图片上时显示图片的alt信息
css实用技巧——异步加载图片时,在图片完成加载前,鼠标悬浮到占位图片上时显示图片的alt信息
18 0
|
前端开发
css跳动文字——加载中
css跳动文字——加载中
212 0
使用5.0.0版本的cssbox将html文件转为图片文件,并解决字体显示问题
使用5.0.0版本的cssbox将html文件转为图片文件,并解决字体显示问题
407 0
|
JavaScript
Vue3——压缩字体font-spider,完美解决字体压缩后会出现字体消失现象
压缩字体font-spider,完美解决字体压缩后会出现字体消失现象
469 0
|
前端开发
清除css默认样式
清除css默认样式
111 0
|
iOS开发
(转载)教你如何在iOS项目中设置各种字体
原文地址:http://www.cnblogs.com/jijiYY/p/4736967.html 在iOS开发中设置字体的方法有很多种,下面为大家介绍比较常用的三种方法 1.
1431 0
使用位图字体工具BMFont从图片生成自定义字体
上一篇转了别人的一篇文章,讲了BMFont的基本用法。对BMFont比较陌生的同学请点击这里先去学习:http://blog.csdn.net/keshuiyun/article/details/9960589。
1529 0
|
Web App开发 前端开发 Windows