您可以使用 UIFontWDCustomLoader 分类在运行时将任何兼容字体加载到您的 iOS 项目中,而无需修改 plist、未知字体名称或奇怪的魔法。
您唯一需要知道的就是您的字体文件名和这个库的名称。
您也可以在应用程序安装后使用这个库来加载新字体。
如果您看不到字体,请检查 构建阶段 > 复制Bundle资源
:您必须在此处看到字体文件名。
#import "UIFont+WDCustomLoader.h"
/* FONT COLLECTION FILE (TTC OR OTC) */
// Create an NSURL for your font file: 'Lao MN.ttc'
NSURL *chalkboardFontURL = [[NSBundle mainBundle] URLForResource:@"Lao MN" withExtension:@"ttc"]];
// Do the registration.
NSArray *fontPostScriptNames = [UIFont registerFontFromURL:chalkboardFontURL];
// 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 *chalkboardFont = [UIFont fontWithName:@"LaoMN" size:18.0f];
或
/* 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"];
/* 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"];
注意: 字体注册将在第一个 [ UIFont customFont… ]
方法调用时进行。
UIFontWDCustomLoader 需要
该库已与 iOS 5、6 和 7 进行了测试
只需从这里下载并将其手动包含到您的项目中。
您有权威的 git submodule
选项。只需在您的仓库根目录中执行以下命令:
git submodule add https://github.com/daktales/UIFontWDCustomLoader.git <path>
git submodule add https://github.com/daktales/UIFontWDCustomLoader.git
该代码根据MIT 许可证的条款和条件分发。
这个库背后的整个想法源于我看到 FlatUIKit 如何加载字体。所以,非常感谢他们。 链接