iOS使用iconfont图标

前奏:先去iconfont里面下载资源
  • 打开下载的文件夹找到 .tff文件直接拖到工程中
    1.jpeg
  • 检查.tff资源文件是否导入成功
    2.jpeg
  • plist文件中加入iconfont字体
    3.jpeg
  • 最后借助淘点点科技写的一个关于iconfont封装,方便我们使用iconfont
    4.jpeg

TBCityIconInfo.h

#import "UIImage+TBCityIconFont.h"
#import "TBCityIconInfo.h"
#define TBCityIconInfoMake(text, imageSize, imageColor) [TBCityIconInfo iconInfoWithText:text size:imageSize color:imageColor]
@interface TBCityIconFont : NSObject
+ (UIFont *)fontWithSize: (CGFloat)size;
+ (void)setFontName:(NSString *)fontName;
@end

TBCityIconFont.m

#import "TBCityIconFont.h"
#import <CoreText/CoreText.h>
@implementation TBCityIconFont
static NSString *_fontName;
+ (void)registerFontWithURL:(NSURL *)url {
    NSAssert([[NSFileManager defaultManager] fileExistsAtPath:[url path]], @"Font file doesn't exist");
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)url);
    CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
    CGDataProviderRelease(fontDataProvider);
    CTFontManagerRegisterGraphicsFont(newFont, nil);
    CGFontRelease(newFont);
}
+ (UIFont *)fontWithSize:(CGFloat)size {
    UIFont *font = [UIFont fontWithName:[self fontName] size:size];
    if (font == nil) {
        NSURL *fontFileUrl = [[NSBundle mainBundle] URLForResource:[self fontName] withExtension:@"ttf"];
        [self registerFontWithURL: fontFileUrl];
        font = [UIFont fontWithName:[self fontName] size:size];
        NSAssert(font, @"UIFont object should not be nil, check if the font file is added to the application bundle and you're using the correct font name.");
    }
    return font;
}
+ (void)setFontName:(NSString *)fontName {
    _fontName = fontName;
}
+ (NSString *)fontName {
    return _fontName ? : @"iconfont";
}
@end

TBCityIconInfo.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface TBCityIconInfo : NSObject
@property (nonatomic, strong) NSString *text;
@property (nonatomic, assign) NSInteger size;
@property (nonatomic, strong) UIColor *color;

- (instancetype)initWithText:(NSString *)text size:(NSInteger)size color:(UIColor *)color;
+ (instancetype)iconInfoWithText:(NSString *)text size:(NSInteger)size color:(UIColor *)color;
@end

TBCityIconInfo.m

#import "TBCityIconInfo.h"
@implementation TBCityIconInfo

- (instancetype)initWithText:(NSString *)text size:(NSInteger)size color:(UIColor *)color {
    if (self = [super init]) {
        self.text = text;
        self.size = size;
        self.color = color;
    }
    return self;
}
+ (instancetype)iconInfoWithText:(NSString *)text size:(NSInteger)size color:(UIColor *)color {
    return [[TBCityIconInfo alloc] initWithText:text size:size color:color];
}
@end

UIImage+TBCityIconFont.h

#import <UIKit/UIKit.h>
#import "TBCityIconInfo.h"
@interface UIImage (TBCityIconFont)
+ (UIImage *)iconWithInfo:(TBCityIconInfo *)info;
@end

UIImage+TBCityIconFont.m

#import "UIImage+TBCityIconFont.h"
#import "TBCityIconFont.h"
#import <CoreText/CoreText.h>
@implementation UIImage (TBCityIconFont)
+ (UIImage *)iconWithInfo:(TBCityIconInfo *)info {
    CGFloat size = info.size;
    CGFloat scale = [UIScreen mainScreen].scale;
    CGFloat realSize = size * scale;
    UIFont *font = [TBCityIconFont fontWithSize:realSize];
    UIGraphicsBeginImageContext(CGSizeMake(realSize, realSize));
    CGContextRef context = UIGraphicsGetCurrentContext();
    if ([info.text respondsToSelector:@selector(drawAtPoint:withAttributes:)]) {
        /**
         * 如果这里抛出异常,请打开断点列表,右击All Exceptions -> Edit Breakpoint -> All修改为Objective-C
         * See: http://stackoverflow.com/questions/1163981/how-to-add-a-breakpoint-to-objc-exception-throw/14767076#14767076
         */
        [info.text drawAtPoint:CGPointZero withAttributes:@{NSFontAttributeName:font, NSForegroundColorAttributeName: info.color}];
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        CGContextSetFillColorWithColor(context, info.color.CGColor);
        [info.text drawAtPoint:CGPointMake(0, 0) withFont:font];
#pragma clang pop
    }
    UIImage *image = [UIImage imageWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:scale orientation:UIImageOrientationUp];
    UIGraphicsEndImageContext();
    return image;
}
@end

ViewController.m中实现
#import "ViewController.h"
#import "TBCityIconFont.h"
#import "UIImage+TBCityIconFont.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *lab;
@property (weak, nonatomic) IBOutlet UIButton *btn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imgView.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e605", 40, [UIColor magentaColor])];
    _lab.font = [UIFont fontWithName:@"iconfont" size:14];
    _lab.text = @"在lable上显示  \U0000e624";
    _lab.textColor = [UIColor purpleColor];
    _btn.titleLabel.font = [UIFont fontWithName:@"iconfont" size:16];
    [_btn setTitle:@"\U0000e622 button with image \U0000e623" forState:UIControlStateNormal];
}
效果图
5.jpeg

注意: iOS使用iconfont官方链接

  • 所用到的unicode编码需要自己手动将&#xXXXX格式转换成\UXXXXXXXX格式,例如//图标编码是,需要转成\U0000e605
  • 所有需要的unicode编码都可以在下载的iconfont文件夹中的.html文件打开查看
  • 下载的iconfont文件夹中有三个.html找到demo_unicode.html这个歌打开
  • 官方也说明了unicode引用iconfont显示的图片只能是纯色,毕竟是用文字形式展示的,这么一想也是比较好理解的。要想显示多色的可以尝试一下使用svg格式。但是iOS系统本身比支持加载这种格式,好在SVGKit这个第三发可以。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.阿里适量图库 地址 http://iconfont.cn/ 2.创建项目 3.下载字体库到本地 4.添加到工程...
    Z小新阅读 4,651评论 4 3
  • 1.什么是iconfont     iconFont拆开来看,就是 Icon + Font,这样估计大家应该都能理...
    JackerooChu阅读 6,798评论 11 21
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,003评论 3 119
  • 这一学期的课文都已经讲完了,这一周语文课安排必读书目的分享会。周三儿子看到其他同学分享的读后感都很精彩,于是...
    随遇而安SZ阅读 144评论 1 3
  • DDL DDL: 数据库定义语句,即对数据库内部对象创建【CREATE】、删除【DROP】、修改【ALTER】等操...
    蚂蚁窝大梦想阅读 165评论 0 1