最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS中如何使用iconfont圖標實例詳解

 更新時間:2018年07月29日 14:12:39   作者:JackerooChu  
iconfont大家在開發(fā)中應該會經(jīng)常用到,下面這篇文章主要給大家介紹了在iOS中如何使用iconfont圖標實例的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧

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地址


如圖我們可以選擇自己需要的icon加入到購物車,然后加入項目里,當然你也可以直接在購物車直接下載,但是這樣只是沒有修改icon為自己想要的樣式,加入項目中,你可以自己任意修改icon為自己想要的樣式。


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

2.將下載下來的icon資源添加到自己的項目中。

我們所需要的就是這個iconfont.ttf,對于這個ttf文件,我想我們并不陌生吧。新建項目,將這個ttf文件拖入自己的項目里。


注意:勾選如圖選項

接下來配置項目加載這個文件

檢查文件是否在項目中,不然會崩潰


在plist文件中加入字體


接下來我們借助淘點點科技寫的一個關于iconfont封裝,方便我們使用iconfont。iconfont的封裝包括


工具類

TBCityIconInfo.h的實現(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

TBCityIconInfo.m的實現(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

TBCityIconFont.h的實現(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;

TBCityIconFont.m的實現(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

UIImage+TBCityIconFont.h的實現(xiàn)

#import <UIKit/UIKit.h>
#import "TBCityIconInfo.h"

@interface UIImage (TBCityIconFont)

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

@end
UIImage+TBCityIconFont.m的實現(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:)]) {
  /**
   * 如果這里拋出異常,請打開斷點列表,右擊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中實現(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];
 //圖標編碼是&#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可以將文字與圖標結合一起,直接用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格式轉(zhuǎn)換成\UXXXXXXXX格式,例如//圖標編碼是&#xe600,需要轉(zhuǎn)成\U0000e600

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


本文demo本地下載),歡迎批評指正

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • iOS中 LGLAlertView 提示框的實例代碼

    iOS中 LGLAlertView 提示框的實例代碼

    這篇文章主要介紹了iOS中 LGLAlertView 提示框的實例代碼非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • iOS新功能引導提示界面實例詳解

    iOS新功能引導提示界面實例詳解

    在開發(fā)中,現(xiàn)在很多app更新了新功能時都會給出用戶一個提示,以方便用戶更好的體驗,那么這個功能如何實現(xiàn)的呢?下面通過本文給大家分享iOS新功能引導提示界面實例詳解,需要的的朋友參考下吧
    2017-04-04
  • iOS開發(fā)避免安全隱患的要點總結

    iOS開發(fā)避免安全隱患的要點總結

    在本篇文章里小編給各位整理了關于iOS開發(fā)如何避免安全隱患的知識點總結,需要的朋友們學習下。
    2019-07-07
  • 查看iOS Crash logs的方法

    查看iOS Crash logs的方法

    發(fā)布了一個應用,用戶使用 的時候crash了,現(xiàn)在想調(diào)查為何crash,所以想在這里探討一下如何查看iphone 手機的crash logs
    2015-06-06
  • Objective-C學習之ARC的實現(xiàn)方法

    Objective-C學習之ARC的實現(xiàn)方法

    自動引用計數(shù)(Automatic Reference Counting, ARC)把壓在程序員們肩頭的管理內(nèi)存的重擔卸除了不少,更不用說讓跟蹤內(nèi)存泄漏那樣的煩心事也少了很多。下面這篇文章主要給大家介紹了關于Objective-C學習之ARC的實現(xiàn)方法,需要的朋友可以參考借鑒下。
    2017-12-12
  • iOS開發(fā)系列--詳細介紹數(shù)據(jù)存取

    iOS開發(fā)系列--詳細介紹數(shù)據(jù)存取

    本篇文章主要介紹了iOS開發(fā)系列--詳細介紹數(shù)據(jù)存取,詳細介紹了IOS數(shù)據(jù)的存儲問題,具有一定的參考價值,有興趣的同學可以了解一下。
    2016-11-11
  • 詳解iOS應用UI開發(fā)中的九宮格坐標計算與字典轉(zhuǎn)換模型

    詳解iOS應用UI開發(fā)中的九宮格坐標計算與字典轉(zhuǎn)換模型

    這篇文章主要介紹了iOS應用UI開發(fā)中的九宮格坐標計算與字典轉(zhuǎn)換模型,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-01-01
  • iOS給border設置漸變色的方法實例

    iOS給border設置漸變色的方法實例

    這篇文章主要給大家介紹了關于iOS給border設置漸變色的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • iOS NSURLSessionDownloadTask實現(xiàn)文件斷點下載的方法

    iOS NSURLSessionDownloadTask實現(xiàn)文件斷點下載的方法

    本篇文章主要介紹了iOS NSURLSessionDownloadTask實現(xiàn)文件斷點下載的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • iOS小組件開發(fā)之WidgetKit功能講解

    iOS小組件開發(fā)之WidgetKit功能講解

    這篇文章主要為大家介紹了iOS小組件開發(fā)WidgetKit功能講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06

最新評論

台东县| 密山市| 邳州市| 武鸣县| 万年县| 益阳市| 芜湖县| 斗六市| 娄底市| 同江市| 三河市| 天门市| 永济市| 中西区| 增城市| 绵阳市| 泰州市| 五莲县| 东兴市| 华宁县| 宝坻区| 广汉市| 定日县| 珲春市| 东宁县| 阜南县| 资源县| 池州市| 板桥市| 沾益县| 聂荣县| 平塘县| 龙口市| 牡丹江市| 邛崃市| 景泰县| 石河子市| 台中县| 平江县| 泽州县| 诸城市|