老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業(yè)提供網站建設、域名注冊、服務器等服務

iOS中使用iconfont圖標的方法

這篇文章主要介紹了iOS中使用iconfont圖標的方法,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯專注為客戶提供全方位的互聯網綜合服務,包含不限于網站設計、成都做網站、元寶網絡推廣、成都小程序開發(fā)、元寶網絡營銷、元寶企業(yè)策劃、元寶品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創(chuàng)新互聯為所有大學生創(chuàng)業(yè)者提供元寶建站搭建服務,24小時服務熱線:18980820575,官方網址:www.cdcxhl.com

1.什么是iconfont

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

2.為什么要使用iconfont

在開發(fā)項目時,不可避免的會用到各種圖標,為了適配不同的設備,通常需要@2x和@3x兩套圖,例如說我們tabBar上使用的圖標。有些app有換膚的需要,還需要多套不同的圖來進行匹配不同的主題。如果使用切圖,這對于設計和開發(fā)來說無疑是增加了工作量,而且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 
#import 

@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 

@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 
#import "TBCityIconInfo.h"

@interface UIImage (TBCityIconFont)

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

@end
UIImage+TBCityIconFont.m的實現
#import "UIImage+TBCityIconFont.h"
#import "TBCityIconFont.h"
#import 

@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];
 //圖標編碼是,需要轉成\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格式,例如//圖標編碼是,需要轉成\U0000e600

2. 所有需要的unicode編碼都可以在下載的iconfont文件夾中的.html文件打開查看

iOS中使用iconfont圖標的方法

感謝你能夠認真閱讀完這篇文章,希望小編分享的“iOS中使用iconfont圖標的方法”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯,關注創(chuàng)新互聯行業(yè)資訊頻道,更多相關知識等著你來學習!


分享文章:iOS中使用iconfont圖標的方法
標題URL:http://www.xueling.net.cn/article/pegeei.html

其他資訊

在線咨詢
服務熱線
服務熱線:028-86922220
TOP
主站蜘蛛池模板: 免费精东传媒vs天美传媒 | 日本免费一区二区三区不卡网 | 国产下面一进一出好爽视频 | 超碰成人在线播放 | 尹人香蕉99久久综合网站 | 国产啊女在线观看 | 在线观看av黄色 | 久久久久成人精品亚洲国产 | 欧美亚洲综合在线观看 | 日本视频中文字幕 | 蜜臀AV无码一区二区三区 | 国产精品高清一区二区不卡片 | 九九免费在线视频 | 久久精品久久精品中文字幕 | 亚洲综合久久无码色噜噜赖水 | 狠狠狠狠狠狠狠狠狠 | chinese国产高清av内谢 | 91精品1区2区 | 国内外免费激情视频 | 超碰在线网站 | 日本va在线视频播放 | 国产精品视频免费看 | 日本亚洲精品无码专区 | 涩涩涩av | 99热这里只有精品99 | 免费在线看片网站 | 曰韩无码AV片免费播放不卡 | 成人无码做爰www免费软件小说 | 亚洲一区二区 | 色综久久综合桃花网国产精品 | 国产色女人 | 免费国无人区码卡二卡 | 亚洲第一黄色网址 | 中文字幕在线观看免费 | 国产区精品 | 内地级a艳片高清免费播放 99热在线播放 | 亚洲xxx视频 | 亚洲欧美另类久久久精品2019 | 成人av一级| 色婷婷人妻av毛片一区 | 91杏吧在线观看 |