iOS中如何使用iconfont图标实例详解

来自:互联网
时间:2020-05-26
阅读:

1.什么是iconfont

iconFont拆开来看,就是 Icon + Font,这样估计大家应该都能理解是什么,那两者结合是什么呢?没错!就是 IconFont !让开发者像使用字体一样使用图标。如果自己不会做的话,可以直接去阿里的iconfont图标库下载自己需要的图标。

2.为什么要使用iconfont

在开发项目时,不可避免的会用到各种图标,为了适配不同的设备,通常需要@2x和@3x两套图,例如说我们tabBar上使用的图标。有些app有换肤的需要,还需要多套不同的图来进行匹配不同的主题。如果使用切图,这对于设计和开发来说无疑是增加了工作量,而且ipa的体积也会增大。

使用iconfont的好处:

1. 减小ipa包的大小

2. 图标保真缩放,多设备适配一套图解决问题

3. 适应换肤要求,使用方便。

3.怎么用iconfont

1. 首先去iconfont图标库下载自己需要的图标。

简书里竟然gif大小限制的这么厉害,所以将动图放到项目里了,需要的在文末有git地址

iOS中如何使用iconfont图标实例详解

如图我们可以选择自己需要的icon加入到购物车,然后加入项目里,当然你也可以直接在购物车直接下载,但是这样只是没有修改icon为自己想要的样式,加入项目中,你可以自己任意修改icon为自己想要的样式。

iOS中如何使用iconfont图标实例详解

注意:这里是下载代码,这样我们就可以在项目中直接使用

2.将下载下来的icon资源添加到自己的项目中。

iOS中如何使用iconfont图标实例详解

我们所需要的就是这个iconfont.ttf,对于这个ttf文件,我想我们并不陌生吧。新建项目,将这个ttf文件拖入自己的项目里。

iOS中如何使用iconfont图标实例详解

注意:勾选如图选项

接下来配置项目加载这个文件

检查文件是否在项目中,不然会崩溃

iOS中如何使用iconfont图标实例详解

在plist文件中加入字体

iOS中如何使用iconfont图标实例详解

接下来我们借助淘点点科技写的一个关于iconfont封装,方便我们使用iconfont。iconfont的封装包括

iOS中如何使用iconfont图标实例详解
工具类

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];
 //图标编码是&#xe600,需要转成\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

结果如下图所示:

iOS中如何使用iconfont图标实例详解

注意:

1. 所用到的unicode编码需要自己手动将&#xXXXX格式转换成\UXXXXXXXX格式,例如//图标编码是&#xe600,需要转成\U0000e600

2. 所有需要的unicode编码都可以在下载的iconfont文件夹中的.html文件打开查看

iOS中如何使用iconfont图标实例详解

本文demo本地下载),欢迎批评指正

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

返回顶部
顶部