将NSString变成贝塞尔曲线

将NSString变成贝塞尔曲线-LMLPHP

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(, , , );
line1.fillColor = [UIColor clearColor].CGColor;
line1.strokeColor = [UIColor blackColor].CGColor;
line1.strokeStart = .f;
line1.strokeEnd = .f;
line1.lineWidth = .f; // 从string上获取到CGPath
line1.path = [UIBezierPath pathFromString:@"游贤明"
WithFont:[UIFont fontWithName:CUSTOM_FONT(@"新蒂小丸子体", )
size:.f]].CGPath;
// 让文字按照正常的顺序显示
line1.bounds = CGPathGetBoundingBox(line1.path);
line1.geometryFlipped = YES; // 设置颜色渐变layer
CAGradientLayer *colorLayer = [CAGradientLayer layer];
colorLayer.frame = CGRectMake(, , , ); // 设置遮罩
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()%/.f;
colorLayer.colors = @[(id)[UIColor colorWithRed:arc4random()%/.f
green:arc4random()%/.f
blue:arc4random()%/.f
alpha:].CGColor,
(id)[UIColor colorWithRed:arc4random()%/.f
green:arc4random()%/.f
blue:arc4random()%/.f
alpha:].CGColor,
(id)[UIColor colorWithRed:arc4random()%/.f
green:arc4random()%/.f
blue:arc4random()%/.f
alpha:].CGColor,
(id)[UIColor colorWithRed:arc4random()%/.f
green:arc4random()%/.f
blue:arc4random()%/.f
alpha:].CGColor,
(id)[UIColor colorWithRed:arc4random()%/.f
green:arc4random()%/.f
blue:arc4random()%/.f
alpha:].CGColor];
} timeInterval:NSEC_PER_SEC];
[_timer start];
} @end

效果:

将NSString变成贝塞尔曲线-LMLPHP

核心代码:

将NSString变成贝塞尔曲线-LMLPHP

附录:

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

05-11 21:53