iOS中优雅的使用iconfont

1.什么是iconfont

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

2.为什么要使用iconfont

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

使用iconfont的好处:

1. 减小ipa包的大小
2. 图标保真缩放,多设备适配一套图解决问题
3. 适应换肤要求,使用方便。
复制代码

3.怎么用iconfont

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

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

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

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

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

注意:勾选如图选项

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

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

  • 在plist文件中加入字体
    接下来我们借助淘点点科技写的一个关于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
复制代码
  1. 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
复制代码
  1. 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;

复制代码
  1. 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
复制代码
  1. UIImage+TBCityIconFont.h的实现
#import <UIKit/UIKit.h>
#import "TBCityIconInfo.h"

@interface UIImage (TBCityIconFont)

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

@end
复制代码
  1. 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;
}
复制代码
  1. 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
复制代码

结果如下图所示:

注意: 1. 所用到的unicode编码需要自己手动将&#xXXXX格式转换成\UXXXXXXXX格式,例如//图标编码是&#xe600,需要转成\U0000e600 2. 所有需要的unicode编码都可以在下载的iconfont文件夹中的.html文件打开查看

本文demo,欢迎批评指正,留下你的star哦。 更多文章请点击这里

如有需要,请关注公众号JackerooChu,了解更多文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS可以使用iconfont来实现图标的显示和使用。 首先,需要将iconfont字体文件导入到项目,可以通过将.ttf或.otf字体文件拖入项目目录或使用CocoaPods等方式进行导入。 然后,在项目的plist文件添加字体文件的配置项,即在`Fonts provided by application`数组加入字体文件的名称。这将使应用程序能够识别并使用该字体。 接下来,可以使用UILabel、UIButton等相关控件来显示和使用iconfont。首先,需要创建一个UILabel或UIButton的实例,并设置其字体为自定义的字体。然后,可以通过设置`text`属性为对应的iconfont编码来显示相应的图标。 在设置iconfont编码时,可以通过将编码转化为Unicode字符串的方式来实现。比如,如果iconfont编码为`\uE001`,则可以通过`@"\uE001"`来设置编码。 此外,还可以使用NSString的方法来显示iconfont。比如,可以使用`stringWithFormat:`方法将编码转化为字符串,并设置其为label或button的text属性。 在使用iconfont时,还可以通过设置字体大小、颜色等方式来调整图标的显示效果。与普通文字一样,可以使用通过设置`label.font`或`button.titleLabel.font`来设置字体大小,通过设置`label.textColor`或`button.titleLabel.textColor`来设置字体颜色。 总之,通过使用iconfontiOS开发者可以轻松地在应用程序使用矢量图标,提供了更灵活、可扩展的图标使用方式,并且在不同的屏幕分辨率下都能保持良好的显示效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值