在 iOS 中使用 icofont 字体

Apr 30, 2019 • 预计阅读时间 1 分钟

获取 icofont

可以在 icofont.com 下载到 icofont.ttf,里面包含了很多图标。

把下载回来的 icofont.ttf 拖到项目里,或者新建一个 fonts.bundle 用来放字体资源。

在 APP 启动的时候注册字体,注册之后就能在 APP 里全局使用了,使用方法和使用系统字体一样。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSBundle *fontsBundle = [NSBundle bundleWithPath:[NSBundle.mainBundle pathForResource:@"fonts" ofType:@"bundle"]];
    NSURL *fontPathUrl = [fontsBundle URLForResource:@"icofont" withExtension:@"ttf"];
    CTFontManagerRegisterFontsForURL((CFURLRef)fontPathUrl, kCTFontManagerScopeProcess, NULL);

    return YES;
}

给 UIFont 添加一个 icofont 字体的 fallback 扩展,指定查找顺序:icofont > 系统字体

@implementation UIFont (icofont)

+ (UIFont *)fontOfSize:(CGFloat)fontSize {
    UIFont *systemFont = [UIFont systemFontOfSize:fontSize];

    NSMutableDictionary *attrs = NSMutableDictionary.dictionary;
    attrs[UIFontDescriptorNameAttribute] = @"icofont";
    attrs[UIFontDescriptorCascadeListAttribute] = @[[UIFontDescriptor fontDescriptorWithName:systemFont.fontName size:fontSize]];

    UIFontDescriptor *fd = [UIFontDescriptor fontDescriptorWithFontAttributes:attrs];

    return [UIFont fontWithDescriptor:fd size:fontSize];
}

+ (UIFont *)boldFontOfSize:(CGFloat)fontSize {
    UIFont *systemFont = [UIFont boldSystemFontOfSize:fontSize];

    NSMutableDictionary *attrs = NSMutableDictionary.dictionary;
    attrs[UIFontDescriptorNameAttribute] = @"icofont";
    attrs[UIFontDescriptorCascadeListAttribute] = @[[UIFontDescriptor fontDescriptorWithName:systemFont.fontName size:fontSize]];

    UIFontDescriptor *fd = [UIFontDescriptor fontDescriptorWithFontAttributes:attrs];

    return [UIFont fontWithDescriptor:fd size:fontSize];
}

+ (UIFont *)italicFontOfSize:(CGFloat)fontSize {
    UIFont *systemFont = [UIFont italicSystemFontOfSize:fontSize];

    NSMutableDictionary *attrs = NSMutableDictionary.dictionary;
    attrs[UIFontDescriptorNameAttribute] = @"icofont";
    attrs[UIFontDescriptorCascadeListAttribute] = @[[UIFontDescriptor fontDescriptorWithName:systemFont.fontName size:fontSize]];

    UIFontDescriptor *fd = [UIFontDescriptor fontDescriptorWithFontAttributes:attrs];

    return [UIFont fontWithDescriptor:fd size:fontSize];
}

@end

测试字体效果

[text insertString:@"\U0000ef71" atIndex:0];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:text
                                                                               attributes:@{
                                                                                             NSFontAttributeName: [UIFont fontOfSize:15],
                                                                                             NSForegroundColorAttributeName: RGB(0x515151)
                                                                                            }];
_textLabel.attributedText = attrString;
iOS
版权声明:如果转发请带上本文链接和注明来源。

lvv.me

iOS/macOS Developer

rsshub with TLS

在 ObjC 项目里使用 Swift 静态库