首页 技术 正文
技术 2022年11月23日
0 收藏 608 点赞 3,355 浏览 2610 个字

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

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

*用的时候直接取

使用 UIFontWDCustomLoader 载入自定义字体

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

使用 UIFontWDCustomLoader 载入自定义字体

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

使用 UIFontWDCustomLoader 载入自定义字体

再来一个组合显示:

使用 UIFontWDCustomLoader 载入自定义字体

结果,效果拔群!!

使用 UIFontWDCustomLoader 载入自定义字体

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,840