iOS上如何使用iconfont圖標(biāo)

1.什么是iconfont

????iconFont拆開(kāi)來(lái)看,就是 Icon + Font,這樣估計(jì)大家應(yīng)該都能理解是什么,那兩者結(jié)合是什么呢?沒(méi)錯(cuò)!就是 IconFont !讓開(kāi)發(fā)者像使用字體一樣使用圖標(biāo)。如果自己不會(huì)做的話,可以直接去阿里的iconfont圖標(biāo)庫(kù)下載自己需要的圖標(biāo)。

2.為什么要使用iconfont

????在開(kāi)發(fā)項(xiàng)目時(shí),不可避免的會(huì)用到各種圖標(biāo),為了適配不同的設(shè)備,通常需要@2x和@3x兩套圖,例如說(shuō)我們tabBar上使用的圖標(biāo)。有些app有換膚的需要,還需要多套不同的圖來(lái)進(jìn)行匹配不同的主題。如果使用切圖,這對(duì)于設(shè)計(jì)和開(kāi)發(fā)來(lái)說(shuō)無(wú)疑是增加了工作量,而且ipa的體積也會(huì)增大。

使用iconfont的好處:

1. 減小ipa包的大小
2. 圖標(biāo)保真縮放,多設(shè)備適配一套圖解決問(wèn)題
3. 適應(yīng)換膚要求,使用方便。

3.怎么用iconfont

1. 首先去iconfont圖標(biāo)庫(kù)下載自己需要的圖標(biāo)。

簡(jiǎn)書(shū)里竟然gif大小限制的這么厲害,所以將動(dòng)圖放到項(xiàng)目里了,需要的在文末有g(shù)it地址

iconfont.jpg

如圖我們可以選擇自己需要的icon加入到購(gòu)物車(chē),然后加入項(xiàng)目里,當(dāng)然你也可以直接在購(gòu)物車(chē)直接下載,但是這樣只是沒(méi)有修改icon為自己想要的樣式,加入項(xiàng)目中,你可以自己任意修改icon為自己想要的樣式。


05.jpeg

注意:這里是下載代碼,這樣我們就可以在項(xiàng)目中直接使用

2.將下載下來(lái)的icon資源添加到自己的項(xiàng)目中。
04.jpeg

我們所需要的就是這個(gè)iconfont.ttf,對(duì)于這個(gè)ttf文件,我想我們并不陌生吧。新建項(xiàng)目,將這個(gè)ttf文件拖入自己的項(xiàng)目里。


03.jpeg

注意:勾選如圖選項(xiàng)

接下來(lái)配置項(xiàng)目加載這個(gè)文件

  • 檢查文件是否在項(xiàng)目中,不然會(huì)崩潰


    02.jpeg
  • 在plist文件中加入字體


    plist.png

接下來(lái)我們借助淘點(diǎn)點(diǎn)科技寫(xiě)的一個(gè)關(guān)于iconfont封裝,方便我們使用iconfont。iconfont的封裝包括


工具類(lèi)
  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
  1. TBCityIconInfo.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
  1. 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;

  1. 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
  1. UIImage+TBCityIconFont.h的實(shí)現(xiàn)
#import <UIKit/UIKit.h>
#import "TBCityIconInfo.h"

@interface UIImage (TBCityIconFont)

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

@end
  1. UIImage+TBCityIconFont.m的實(shí)現(xiàn)
#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

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中實(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];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 30, 30)];
    [self.view addSubview:imageView];
    //圖標(biāo)編碼是&#xe600,需要轉(zhuǎn)成\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可以將文字與圖標(biāo)結(jié)合一起,直接用label的text屬性將圖標(biāo)顯示出來(lái)
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 280, 40)];
    [self.view addSubview:label];
    label.font = [UIFont fontWithName:@"iconfont" size:15];//設(shè)置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

結(jié)果如下圖所示:


01.jpeg

注意:
1. 所用到的unicode編碼需要自己手動(dòng)將&#xXXXX格式轉(zhuǎn)換成\UXXXXXXXX格式,例如//圖標(biāo)編碼是&#xe600,需要轉(zhuǎn)成\U0000e600
2. 所有需要的unicode編碼都可以在下載的iconfont文件夾中的.html文件打開(kāi)查看


code.jpeg

本文demo,歡迎批評(píng)指正,留下你的star哦。更多文章請(qǐng)點(diǎn)擊這里

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,725評(píng)論 25 709
  • iconfont是阿里旗下的矢量圖標(biāo)庫(kù),因?yàn)槭噶繄D標(biāo)放大縮小不失真,以及其方便修改,容量小等特點(diǎn)被廣泛使用。接下來(lái)...
    hibigger閱讀 1,838評(píng)論 0 2
  • 前言 本篇文章其實(shí)陸陸續(xù)續(xù)寫(xiě)了快半年,主體部分寫(xiě)好了很久了,但由于種種原因一直沒(méi)有發(fā)布。首先來(lái)說(shuō)說(shuō)寫(xiě)這篇文章的主要...
    7cd975786ccd閱讀 4,558評(píng)論 0 11
  • 公司在前一段時(shí)間將大量圖標(biāo)進(jìn)行了iconfont的替換,大大縮減了app的size。這幾天,尋思著在自己app上也...
    木木烈少閱讀 2,165評(píng)論 5 11
  • 2.25日精進(jìn):敬畏—進(jìn)入—體驗(yàn)—交給—持續(xù) 1,缺啥補(bǔ)啥,怕啥練啥; 2,一切為我所用,所用為團(tuán)隊(duì)家; 3,我想...
    京心達(dá)張秀寶閱讀 273評(píng)論 0 0

友情鏈接更多精彩內(nèi)容