参考:
http://www.jianshu.com/p/dd01072998c5
http://www.jianshu.com/p/3b10bb95b332
http://www.jianshu.com/p/c3405c6a58e4
一、什么是IconFont?
1.介绍
Icon(图标),Font(字体),顾名思义图标隐藏在字体文件里面,通过使用字体的方式显示图片,制作好的iconfont图标是一种.ttf格式的字体。Icon font 就是将一套图标集以字体文件的形式封装,一套 icon font 的数目不多,所以字体文件的体积亦会较小,同时因 icon font 中的图标为矢量,所以应对缩放也更为便利。
2.优缺点
使用Icon Font能给我们带来哪些好处,又存在哪些弊端呢?
利:
轻松解决图标资源适配问题,无论什么分辨率,图片都是超级清晰的
轻松改变颜色,大小,背景,圆角,轮廓各种属性
apk大小,小,小,小
可实现跨平台,一套资源,web,ios,android都能用
资源维护很方便
弊:
需要自定义svg图片,并将其转换为ttf文件,图标制作成本比较高
总而言之,我觉着利是大于弊的,做完之后是一劳永逸的,而且大多数图标都不需要我们自己做,网上都有现成的。
比如:
https://github.com/mikepenz/Android-Iconics
http://www.iconfont.cn
3.生产IconFont
fontello为我们提供了将svg图标转换为字体文件的方式,至于svg图片的制作,可以参考:http://www.iconsvg.com,网络上有许多图库。
二、Android中使用IconFont
参考项目:https://github.com/mikepenz/Android-Iconics
1.将ttf字体文件放在assets/fonts目录下
2.在你的Application里面注册字体文件
@Override
public void onCreate() {
super.onCreate();
Iconics.init(getApplicationContext());
//register custom fonts like this (or also provide a font definition file)
Iconics.registerFont(new CustomFont());
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(IconicsContextWrapper.wrap(base));
}
3.CustomFont类,在里面自定义你的Icon Font
public static enum Icon implements IIcon {
met_windy_rain_inv('\ue800'),
met_snow_inv('\ue801'),
met_snow_heavy_inv('\ue802'),
met_hail_inv('\ue803'),
met_clouds_inv('\ue804'),
met_clouds_flash_inv('\ue805'),
met_temperature('\ue806'),
met_compass('\ue807'),
met_na('\ue808'),
met_celcius('\ue809'),
met_fahrenheit('\ue80a'),
}
4.在xml文件中调用定义的Icon Font
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
app:ico_color="@color/md_red_A200"
app:ico_icon="gmd-plus-circle"
app:ico_size="48dp" />
5.在代码中调用定义的Icon Font
new IconicsDrawable(this)
.icon(FontAwesome.Icon.faw_android)
.color(Color.RED)
.sizeDp(24)
三、IOS中使用IconFont
1.添加.ttf文件
创建一个Demo,然后后把iconfont.ttf拖入工程中
2.一些代码封装
TBCityIconInfo的实现
#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
#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的实现
#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
#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的实现
#import <UIKit/UIKit.h>
#import "TBCityIconInfo.h"
@interface UIImage (TBCityIconFont)
+ (UIImage *)iconWithInfo:(TBCityIconInfo *)info;
@end
#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
在AppDelegate.m中,初始化我们的iconfont
#import "AppDelegate.h"
#import "TBCityIconFont.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//iconfont图标
[TBCityIconFont setFontName:@"iconfont"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];
_window.rootViewController = nav;
// Override point for customization after application launch.
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];
self.navigationController.navigationBar.translucent = NO;
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithImage:[ UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e602",22,[UIColor colorWithRed:0.55 green:0.55 blue:0.55 alpha:1])] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
self.navigationItem.leftBarButtonItem = leftBarButton;
self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:0.55 green:0.55 blue:0.55 alpha:1];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e60d",25, [UIColor colorWithRed:0.14 green:0.61 blue:0.83 alpha:1.00])] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:0.14 green:0.61 blue:0.83 alpha:1.00];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)loadView{
[super loadView];
// imageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 50, 30, 30)];
[self.view addSubview:imageView];
//图标编码是,需要转成\U0000e603
imageView.image = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e603", 30, [UIColor redColor])];
// button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 40, 40);
[self.view addSubview:button];
[button setImage:[UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e60c", 40, [UIColor redColor])] forState:UIControlStateNormal];
// label,label可以将文字与图标结合一起,直接用label的text属性将图标显示出来
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 160, 280, 40)];
[self.view addSubview:label];
label.font = [UIFont fontWithName:@"iconfont" size:15];//设置label的字体
label.text = @"这是用label显示的iconfont \U0000e60c";
}
-(void)leftButtonAction{
}
-(void)rightButtonAction{
}