将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

目录
相关文章
UIImageView加上圆角
UIImageView加上圆角
39 0
UITableView 凹注意事项
UITableView : 一、注意方法实现顺序, table.tableFooterView 或者 table.tableHeaderView 提前2行你将看到感觉不太好的UI。
801 0
|
UED 缓存 异构计算
UIImageView添加圆角的几种方法
喜欢我的可以关注收藏我的个人博客:RobberJJ 创建一个UIImageView对象: UIImageView * poImgView = [[UIImageView alloc] init]; 第一种方法 poImgView.
942 0