iOS 如何使用第三方字库

1.第一步找到你想用的字体的 ttf 格式。加入到你的工程的resouce目录下。

2.在工程的plist中AddRow,“Fonts provided by application” ,然后添加key为item0,value为你刚才加入的testFont.ttf 。

是这样,可以添加多个,使用的时候写对应字体名字就行。

3.在你的工程就可以直接用了。xx.font = [UIFont fontWithName:@"testFont" size:20.0];

 
 
但是一般来说,字体文件比较大,不该内置,而且如果都用plist预定义的方式,那肯定就没法覆盖全,导致用户不能使用更多自己喜欢的字体。所以应该用代码读取字体的方式:
 
提供字体文件路径,返回所需要字体:
 
复制代码
 

添加CoreText字库   导入#import "CoreText/CoreText.h"头文件

-(UIFont*)customFontWithPath:(NSString*)path size:(CGFloat)size
{
    NSURL *fontUrl = [NSURL fileURLWithPath:path];
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fontUrl);
    CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
    CGDataProviderRelease(fontDataProvider);
    CTFontManagerRegisterGraphicsFont(fontRef, NULL);
    NSString *fontName = CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
    UIFont *font = [UIFont fontWithName:fontName size:size];
    CGFontRelease(fontRef);
    return font;
}
原文地址:https://www.cnblogs.com/woaixixi/p/5590426.html