在開發iOS項目時,不可避免的會用到圖標,而為了適配不同分辨率的設備,我們通常會需要@2x,@3x兩套格式的圖片,最明顯的就是底部tabBar的圖標使用。而對于那些有換膚需求的APP來說,還需要多套圖來匹配不同的主題。通過切圖的方式制作圖標,一方面加大了開發者和設計者的工作量,另一方面也會增大APP的體積。而使用iconfont的可以達到以下目的
1.減小應用體積,字體文件比圖片要小;
2.圖標保真縮放,解決2x/3x乃至將來nx圖問題;
3.方便更改圖標顏色大小,圖片復用。
所以為了給開發者、設計者稍微減少點工作量,給APP“減重,我們可以將iconfont應用到自己的項目中。那么,iconfont是怎么用的呢?iconfont,從字面上就能理解它就是字體,讓開發者像使用字體一樣使用圖標。
由于我是做開發的,所以對于iconfont的制作并不太熟悉,都是設計師做好了圖標給我,如果你想學習iconfont的制作的話,可以去阿里巴巴的iconfont平臺去看看,上面有比較全的資料。制作好的iconfont圖標是一種.ttf格式的字體,如圖:
iconfont中的圖標是這樣的:
而我們需要的是將.ttf格式的文件引入到自己的工程中
接下來我們借助淘點點科技寫的一個關于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
2.TBCityFontImageInfo.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
3.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;
@end
4.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
5.UIImage+TBCityIconFont.h的實現
#import <UIKit/UIKit.h>
#import "TBCityIconInfo.h"
@interface UIImage (TBCityIconFont)
+ (UIImage *)iconWithInfo:(TBCityIconInfo *)info;
@end
6.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
7.在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;
}
8.在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{
}
9.運行得到的結果如圖:
這樣,我們就可以很方便的使用iconfont圖標了。這里要注意的是,圖標是用的iconfont中的圖標用的是unicode編碼,我們在自己的工程中時需要將&#xXXXX格式轉換成\UXXXXXXXX格式。