在開(kāi)發(fā)iOS項(xiàng)目時(shí)醇疼,不可避免的會(huì)用到圖標(biāo)朴艰,而為了適配不同分辨率的設(shè)備,我們通常會(huì)需要@2x址愿,@3x兩套格式的圖片该镣,最明顯的就是底部tabBar的圖標(biāo)使用。而對(duì)于那些有換膚需求的APP來(lái)說(shuō)响谓,還需要多套圖來(lái)匹配不同的主題损合。通過(guò)切圖的方式制作圖標(biāo)省艳,一方面加大了開(kāi)發(fā)者和設(shè)計(jì)者的工作量,另一方面也會(huì)增大APP的體積嫁审。而使用iconfont的可以達(dá)到以下目的
1.減小應(yīng)用體積跋炕,字體文件比圖片要小律适;
2.圖標(biāo)保真縮放辐烂,解決2x/3x乃至將來(lái)nx圖問(wèn)題;
3.方便更改圖標(biāo)顏色大小捂贿,圖片復(fù)用棉圈。
所以為了給開(kāi)發(fā)者、設(shè)計(jì)者稍微減少點(diǎn)工作量眷蜓,給APP“減重分瘾,我們可以將iconfont應(yīng)用到自己的項(xiàng)目中。那么吁系,iconfont是怎么用的呢德召?iconfont,從字面上就能理解它就是字體汽纤,讓開(kāi)發(fā)者像使用字體一樣使用圖標(biāo)上岗。
由于我是做開(kāi)發(fā)的,所以對(duì)于iconfont的制作并不太熟悉蕴坪,都是設(shè)計(jì)師做好了圖標(biāo)給我肴掷,如果你想學(xué)習(xí)iconfont的制作的話,可以去阿里巴巴的iconfont平臺(tái)去看看背传,上面有比較全的資料呆瞻。制作好的iconfont圖標(biāo)是一種.ttf格式的字體,如圖:
iconfont中的圖標(biāo)是這樣的:
而我們需要的是將.ttf格式的文件引入到自己的工程中
接下來(lái)我們借助淘點(diǎn)點(diǎn)科技寫(xiě)的一個(gè)關(guān)于iconfont封裝径玖,方便我們使用iconfont痴脾。iconfont的封裝包括
1.TBCityIconInfo.h的實(shí)現(xiàn)
#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的實(shí)現(xiàn)
#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的實(shí)現(xiàn)
#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的實(shí)現(xiàn)
#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的實(shí)現(xiàn)
#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:)]) {
/**
* 如果這里拋出異常,請(qǐng)打開(kāi)斷點(diǎn)列表梳星,右擊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圖標(biāo)
[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中實(shí)現(xiàn)
#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];
//圖標(biāo)編碼是,需要轉(zhuǎn)成\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可以將文字與圖標(biāo)結(jié)合一起冤灾,直接用label的text屬性將圖標(biāo)顯示出來(lái)
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 160, 280, 40)];
[self.view addSubview:label];
label.font = [UIFont fontWithName:@"iconfont" size:15];//設(shè)置label的字體
label.text = @"這是用label顯示的iconfont \U0000e60c";
}
-(void)leftButtonAction{
}
-(void)rightButtonAction{
}
9.運(yùn)行得到的結(jié)果如圖:
這樣前域,我們就可以很方便的使用iconfont圖標(biāo)了。這里要注意的是韵吨,圖標(biāo)是用的iconfont中的圖標(biāo)用的是unicode編碼匿垄,我們?cè)谧约旱墓こ讨袝r(shí)需要將&#xXXXX格式轉(zhuǎn)換成\UXXXXXXXX格式。