使用FontAwesome

简介:

使用FontAwesome

https://github.com/PrideChung/FontAwesomeKit

 

为什么要使用FontAwesome呢,其实,它的字体就是矢量图,无论是放大还是缩小都不失真的矢量图哦.

1. 下载源码,导入文件夹FontAwesomeKit,然后引入头文件FontAwesomeKit.h

 

2. 使用

// 取得固定的icon以及设定尺寸
    FAKZocial *twitterIcon = [FAKZocial stackoverflowIconWithSize:50];
    
    // 设定相关的属性
    [twitterIcon addAttribute:NSForegroundColorAttributeName
                     value:[UIColor redColor]];
    
    // 在UILabel上显示
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    label.attributedText = [twitterIcon attributedString];
    [self.view addSubview:label];
    label.center = self.view.center;

使用是非常简单的哦,效果如下:

 

3. 高级应用

你以为就显示出来就没了么?非也,你想过把字体转换为路径么,转换为路径后就可以执行各种CoreAnimation的动画了呢:)

先来试一下CAshapeLayer的动画路径:

// 取得固定的icon以及设定尺寸
    FAKZocial *twitterIcon = [FAKZocial chromeIconWithSize:100];
    
    // 设定相关的属性
    [twitterIcon addAttribute:NSForegroundColorAttributeName
                     value:[UIColor blackColor]];

    // 将icon转换为贝塞尔曲线
    UIBezierPath *path = [UIBezierPath pathFromAttributedString:[twitterIcon attributedString]];

    // 创建shapeLayer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    // 获取path
    shapeLayer.path = path.CGPath;
    
    // 根据这个path来设定尺寸
    shapeLayer.bounds = CGPathGetBoundingBox(shapeLayer.path);
    
    // 几何反转
    shapeLayer.geometryFlipped = YES;
    
    // 一些颜色的填充
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor cyanColor].CGColor;
    
    // 设定layer位置
    shapeLayer.position = self.view.center;
    [self.view.layer addSublayer:shapeLayer];
    
    // 定时器动画
    _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
    [_timer event:^{
        shapeLayer.strokeEnd = arc4random()%100/100.f;
    } timeInterval:NSEC_PER_SEC];
    [_timer start];


// 取得固定的icon以及设定尺寸
    FAKZocial *twitterIcon = [FAKZocial chromeIconWithSize:100];
    
    // 设定相关的属性
    [twitterIcon addAttribute:NSForegroundColorAttributeName
                     value:[UIColor blackColor]];

    // 将icon转换为贝塞尔曲线
    UIBezierPath *path = [UIBezierPath pathFromAttributedString:[twitterIcon attributedString]];

    // 创建shapeLayer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    // 获取path
    shapeLayer.path = path.CGPath;
    
    // 根据这个path来设定尺寸
    shapeLayer.bounds = CGPathGetBoundingBox(shapeLayer.path);
    
    // 几何反转
    shapeLayer.geometryFlipped = YES;
    
    // 一些颜色的填充
    shapeLayer.fillColor = [UIColor blackColor].CGColor;
    shapeLayer.strokeColor = [UIColor clearColor].CGColor;
    shapeLayer.position = CGPointMake(50, 50);
    
    // 渐变颜色图层
    CAGradientLayer *colorLayer = [CAGradientLayer layer];
    colorLayer.bounds = CGRectMake(0, 0, 120, 120);
    colorLayer.mask = shapeLayer;
    colorLayer.colors = @[(id)[UIColor redColor].CGColor,
                          (id)[UIColor greenColor].CGColor,
                          (id)[UIColor yellowColor].CGColor];
    colorLayer.position = self.view.center;
    
    // 设定layer位置
    [self.view.layer addSublayer:colorLayer];
    
    // 定时器动画
    _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
    [_timer event:^{
        colorLayer.speed = 0.5;
        colorLayer.colors = @[(id)[UIColor colorWithRed:arc4random()%255/255.f
                                                  green:arc4random()%255/255.f
                                                   blue:arc4random()%255/255.f
                                                  alpha:1].CGColor,
                              (id)[UIColor colorWithRed:arc4random()%255/255.f
                                                  green:arc4random()%255/255.f
                                                   blue:arc4random()%255/255.f
                                                  alpha:1].CGColor,
                              (id)[UIColor colorWithRed:arc4random()%255/255.f
                                                  green:arc4random()%255/255.f
                                                   blue:arc4random()%255/255.f
                                                  alpha:1].CGColor,
                              (id)[UIColor colorWithRed:arc4random()%255/255.f
                                                  green:arc4random()%255/255.f
                                                   blue:arc4random()%255/255.f
                                                  alpha:1].CGColor,
                              (id)[UIColor colorWithRed:arc4random()%255/255.f
                                                  green:arc4random()%255/255.f
                                                   blue:arc4random()%255/255.f
                                                  alpha:1].CGColor];
    } timeInterval:NSEC_PER_SEC];
    [_timer start];


// 取得固定的icon以及设定尺寸
    FAKZocial *twitterIcon = [FAKZocial chromeIconWithSize:100];
    
    // 设定相关的属性
    [twitterIcon addAttribute:NSForegroundColorAttributeName
                     value:[UIColor blackColor]];

    // 将icon转换为贝塞尔曲线
    UIBezierPath *path = [UIBezierPath pathFromAttributedString:[twitterIcon attributedString]];

    // 创建shapeLayer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    // 获取path
    shapeLayer.path = path.CGPath;
    
    // 根据这个path来设定尺寸
    shapeLayer.bounds = CGPathGetBoundingBox(shapeLayer.path);
    
    // 几何反转
    shapeLayer.geometryFlipped = YES;
    
    // 一些颜色的填充
    shapeLayer.fillColor = [UIColor redColor].CGColor;
    shapeLayer.strokeColor = [UIColor clearColor].CGColor;
    shapeLayer.position = CGPointMake(50, 50);
    
    // 渐变颜色图层
    CAGradientLayer *colorLayer = [CAGradientLayer layer];
    colorLayer.bounds = CGRectMake(0, 0, 120, 120);
    colorLayer.mask = shapeLayer;
    colorLayer.colors = @[(id)[UIColor redColor].CGColor,
                          (id)[UIColor greenColor].CGColor,
                          (id)[UIColor yellowColor].CGColor];
    colorLayer.position = self.view.center;
    
    // 设定layer位置
    [self.view.layer addSublayer:colorLayer];
    
    // 旋转
    CABasicAnimation *basicAni = \
        [CABasicAnimationList animationWithRotationZFromValue:-2*M_PI_2 toValue:2*M_PI_2];
    basicAni.duration = 1.0f;
    basicAni.repeatCount = HUGE_VALF;
    [shapeLayer addAnimation:basicAni forKey:nil];
    
    // 定时器动画
    _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
    [_timer event:^{
        colorLayer.colors = @[(id)[UIColor colorWithRed:arc4random()%255/255.f
                                                  green:arc4random()%255/255.f
                                                   blue:arc4random()%255/255.f
                                                  alpha:1].CGColor,
                              (id)[UIColor colorWithRed:arc4random()%255/255.f
                                                  green:arc4random()%255/255.f
                                                   blue:arc4random()%255/255.f
                                                  alpha:1].CGColor,
                              (id)[UIColor colorWithRed:arc4random()%255/255.f
                                                  green:arc4random()%255/255.f
                                                   blue:arc4random()%255/255.f
                                                  alpha:1].CGColor,
                              (id)[UIColor colorWithRed:arc4random()%255/255.f
                                                  green:arc4random()%255/255.f
                                                   blue:arc4random()%255/255.f
                                                  alpha:1].CGColor,
                              (id)[UIColor colorWithRed:arc4random()%255/255.f
                                                  green:arc4random()%255/255.f
                                                   blue:arc4random()%255/255.f
                                                  alpha:1].CGColor];
        
    } timeInterval:NSEC_PER_SEC];
    [_timer start];

附录:

    FAKFontAwesome *starIcon = [FAKFontAwesome asteriskIconWithSize:50];
    FAKFoundationIcons *bookmarkIcon = [FAKFoundationIcons bookmarkIconWithSize:15];
    FAKZocial *twitterIcon = [FAKZocial twitterIconWithSize:15];
    FAKIonIcons *mailIcon = [FAKIonIcons ios7EmailIconWithSize:48];

目录
相关文章
|
5月前
|
前端开发
关于 FontAwesome icon 的 before 伪元素
关于 FontAwesome icon 的 before 伪元素
|
5月前
|
前端开发 开发者
关于 FontAwesome 字体图标库的 content 属性
关于 FontAwesome 字体图标库的 content 属性
|
10月前
|
前端开发
iconfont字体图标的使用方法
iconfont字体图标的使用方法
|
C# 容器 Android开发
WPF字体图标——IconFont
原文:WPF字体图标——IconFont 版权声明:本文为【CSDN博主:松一160】原创文章,未经允许不得转载。 https://blog.csdn.net/songyi160/article/details/54894233 一、字体图标概述 ①字体图标其实就是把矢量图形打包到字体文件里,以后就可以像使用一般外置字体一样的使用它,因此Winform、WPF中都是可以用的。
2494 0
|
存储 JavaScript
ICONFONT字体图标库使用
ICONFONT字体图标库使用
292 0
|
前端开发 CDN
Font-Awesome4.7.0版本字体图标显示问题
在Font-Awesome4.7.0版本有时可能会出现字体图标显示为空白小方框的情况,本文提供一个解决思路
1242 0
Font-Awesome4.7.0版本字体图标显示问题
|
前端开发
CSS3中引入多种自定义字体(font-face)
CSS3中引入多种自定义字体(font-face)
443 0
CSS3中引入多种自定义字体(font-face)
|
前端开发
1、CSS中iconfont 彩色图标使用详解
1、CSS中iconfont 彩色图标使用详解
381 0
1、CSS中iconfont 彩色图标使用详解
|
人工智能 前端开发 iOS开发
CSS——web字体与CSS字体图标
CSS——web字体与CSS字体图标
184 0
CSS——web字体与CSS字体图标
|
前端开发
CSS - Iconfont
CSS - Iconfont
244 0
CSS - Iconfont