iOS中优雅的使用iconfont

1.什么是iconfont

    iconFont拆开来看,就是 Icon + Font,这样估计你们应该都能理解是什么,那二者结合是什么呢?没错!就是 IconFont !让开发者像使用字体同样使用图标。若是本身不会作的话,能够直接去阿里的iconfont图标库下载本身须要的图标。html

2.为何要使用iconfont

    在开发项目时,不可避免的会用到各类图标,为了适配不一样的设备,一般须要@2x和@3x两套图,例如说咱们tabBar上使用的图标。有些app有换肤的须要,还须要多套不一样的图来进行匹配不一样的主题。若是使用切图,这对于设计和开发来讲无疑是增长了工做量,并且ipa的体积也会增大。git

使用iconfont的好处:

1. 减少ipa包的大小
2. 图标保真缩放,多设备适配一套图解决问题
3. 适应换肤要求,使用方便。
复制代码

3.怎么用iconfont

1. 首先去iconfont图标库下载本身须要的图标。

下载图片

如图咱们能够选择本身须要的icon加入到购物车,而后加入项目里,固然你也能够直接在购物车直接下载,可是这样只是没有修改icon为本身想要的样式,加入项目中,你能够本身任意修改icon为本身想要的样式。github

1.png

注意:这里是下载代码,这样咱们就能够在项目中直接使用bash

2.将下载下来的icon资源添加到本身的项目中。

2.png
咱们所须要的就是这个iconfont.ttf,对于这个ttf文件,我想咱们并不陌生吧。新建项目,将这个ttf文件拖入本身的项目里。

3.png

注意:勾选如图选项app

接下来配置项目加载这个文件ide

  • 检查文件是否在项目中,否则会崩溃

4.png

  • 在plist文件中加入字体
    plist
    接下来咱们借助淘点点科技写的一个关于iconfont封装,方便咱们使用iconfont。iconfont的封装包括
    工具类
  1. 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
复制代码
  1. 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
复制代码
  1. TBCityIconFont.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;

复制代码
  1. 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
复制代码
  1. UIImage+TBCityIconFont.h的实现
#import <UIKit/UIKit.h>
#import "TBCityIconInfo.h"

@interface UIImage (TBCityIconFont)

+ (UIImage *)iconWithInfo:(TBCityIconInfo *)info;

@end
复制代码
  1. 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

复制代码
3.具体使用方法

1.在AppDelegate.m中,初始化iconfont工具

#import "AppDelegate.h"
#import "TBCityIconFont.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [TBCityIconFont setFontName:@"iconfont"];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];
   self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}
复制代码
  1. ViewController.m中实现
#import "ViewController.h"
#import "TBCityIconFont.h"
#import "UIImage+TBCityIconFont.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor  whiteColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 30, 30)];
    [self.view addSubview:imageView];
    //图标编码是&#xe600,须要转成\U0000e600
    imageView.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e600", 30, [UIColor redColor])];
    
    
    //    button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 150, 40, 40);
    [self.view addSubview:button];
    [button setImage:[UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e614", 40, [UIColor redColor])] forState:UIControlStateNormal];
    
    //    label,label能够将文字与图标结合一块儿,直接用label的text属性将图标显示出来
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 280, 40)];
    [self.view addSubview:label];
    label.font = [UIFont fontWithName:@"iconfont" size:15];//设置label的字体
    label.text = @"在lable上显示 \U0000e658";
    
    

    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
复制代码

结果以下图所示:字体

5.png

注意: 1. 所用到的unicode编码须要本身手动将&#xXXXX格式转换成\UXXXXXXXX格式,例如//图标编码是&#xe600,须要转成\U0000e600 2. 全部须要的unicode编码均可以在下载的iconfont文件夹中的.html文件打开查看 ui

6.png

本文demo,欢迎批评指正,留下你的star哦。 更多文章请点击这里编码

若有须要,请关注公众号JackerooChu,了解更多文章

公众号
相关文章
相关标签/搜索