美文网首页
iOS使用iconfont图标

iOS使用iconfont图标

作者: 梦里桃花舞倾城 | 来源:发表于2018-08-30 13:34 被阅读128次
前奏:先去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这个第三发可以。

相关文章

网友评论

      本文标题:iOS使用iconfont图标

      本文链接:https://www.haomeiwen.com/subject/kiurwftx.html