zoukankan      html  css  js  c++  java
  • 将NSString变成贝塞尔曲线

    将NSString变成贝塞尔曲线

    https://github.com/aderussell/string-to-CGPathRef

    NSString中的字符串是可以通过CoreText框架将其转换成贝塞尔曲线的.

    源码:

    //
    //  RootViewController.m
    //  StringPath
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "RootViewController.h"
    #import "UIBezierPath+TextPaths.h"
    #import "FontPool.h"
    #import "YXGCD.h"
    
    @interface RootViewController ()
    
    @property (nonatomic, strong) GCDTimer *timer;
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor blackColor];
    
        // 注册字体
        [FontPool registerFont:bundleFont(@"新蒂小丸子体.ttf")
                      withName:@"新蒂小丸子体"];
        
        // 获取形状的layer
        CAShapeLayer *line1 = [CAShapeLayer new];
        line1.frame = CGRectMake(0, 0, 200, 200);
        line1.fillColor = [UIColor clearColor].CGColor;
        line1.strokeColor = [UIColor blackColor].CGColor;
        line1.strokeStart = 0.f;
        line1.strokeEnd   = 1.f;
        line1.lineWidth   = 1.f;
        
        // 从string上获取到CGPath
        line1.path = [UIBezierPath pathFromString:@"游贤明"
                                         WithFont:[UIFont fontWithName:CUSTOM_FONT(@"新蒂小丸子体", 0)
                                                                  size:60.f]].CGPath;
        // 让文字按照正常的顺序显示
        line1.bounds = CGPathGetBoundingBox(line1.path);
        line1.geometryFlipped = YES;
        
        // 设置颜色渐变layer
        CAGradientLayer *colorLayer = [CAGradientLayer layer];
        colorLayer.frame = CGRectMake(0, 0, 200, 200);
        
        // 设置遮罩
        colorLayer.mask = line1;
        colorLayer.colors = @[(id)[UIColor redColor].CGColor,
                              (id)[UIColor cyanColor].CGColor];
        colorLayer.position = self.view.center;
        [self.view.layer addSublayer:colorLayer];
        
        // 动画事件
        _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
        [_timer event:^{
            line1.strokeEnd = arc4random()%100/100.f;
            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];
    }
    
    @end

    效果:

    核心代码:

    附录:

    http://stackoverflow.com/questions/7573383/how-to-create-a-multiline-string-or-multiline-label-as-a-cgpath-in-ios

  • 相关阅读:
    命令提示符窗口adb shell 执行sqlite命令时进入 ...> 状态如何退出
    通过android studio 浏览模拟器中文件
    vim快捷键参考
    快速体验openstack-用devstack安装openstack
    css靠左,靠右
    全国-城市-百度地图中心点坐标
    java_dom4j解析xml
    December 31st, Week 53rd Tuesday, 2019
    December 28th, Week 52nd Saturday, 2019
    December 21st, Week 51st Saturday, 2019
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3754542.html
Copyright © 2011-2022 走看看