1.什么是iconfont
iconFont拆开来看,就是 Icon + Font,这样估计大家应该都能理解是什么,那两者结合是什么呢?没错!就是 IconFont !让开发者像使用字体一样使用图标。如果自己不会做的话,可以直接去阿里的iconfont图标库下载自己需要的图标。
2.为什么要使用iconfont
在开发项目时,不可避免的会用到各种图标,为了适配不同的设备,通常需要@2x和@3x两套图,例如说我们tabBar上使用的图标。有些app有换肤的需要,还需要多套不同的图来进行匹配不同的主题。如果使用切图,这对于设计和开发来说无疑是增加了工作量,而且ipa的体积也会增大。
使用iconfont的好处:
1. 减小ipa包的大小
2. 图标保真缩放,多设备适配一套图解决问题
3. 适应换肤要求,使用方便。
复制代码
3.怎么用iconfont
1. 首先去iconfont图标库下载自己需要的图标。
如图我们可以选择自己需要的icon加入到购物车,然后加入项目里,当然你也可以直接在购物车直接下载,但是这样只是没有修改icon为自己想要的样式,加入项目中,你可以自己任意修改icon为自己想要的样式。
注意:这里是下载代码,这样我们就可以在项目中直接使用
2.将下载下来的icon资源添加到自己的项目中。
注意:勾选如图选项
接下来配置项目加载这个文件
- 检查文件是否在项目中,不然会崩溃
- 在plist文件中加入字体
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
复制代码
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;
复制代码
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
复制代码
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
复制代码
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;
}
复制代码
- 在
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];
//图标编码是,需要转成\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
复制代码
结果如下图所示:
注意: 1. 所用到的unicode编码需要自己手动将&#xXXXX格式转换成\UXXXXXXXX格式,例如//图标编码是,需要转成\U0000e600 2. 所有需要的unicode编码都可以在下载的iconfont文件夹中的.html文件打开查看
如有需要,请关注公众号JackerooChu,了解更多文章