将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
效果:
核心代码:
附录: