新工作的第一天,我被要求帮助推出iPad应用程序。他们让我沿曲线路径绘制一个NSString(曲线有点像S形)。
在没有循环字符串的字母并在旋转和移动框架后单独绘制每个字母时,不确定如何做到这一点。有没有更有效的方法呢?
这是我到目前为止所拥有的:
-(void)doCurveString:(NSString *)stringToCurve{
UIGraphicsBeginImageContext(CGSizeMake(768, 1024));
NSArray *arrayFullOfCGRects = [NSArray arrayWithObjects:
[NSValue valueWithCGRect:CGRectMake(300, 500, 20, 20)],
[NSValue valueWithCGRect:CGRectMake(315, 510, 20, 20)],
[NSValue valueWithCGRect:CGRectMake(330, 500, 20, 20)],
[NSValue valueWithCGRect:CGRectMake(345, 510, 20, 20)],
[NSValue valueWithCGRect:CGRectMake(360, 500, 20, 20)],
[NSValue valueWithCGRect:CGRectMake(375, 510, 20, 20)],
[NSValue valueWithCGRect:CGRectMake(390, 500, 20, 20)],
[NSValue valueWithCGRect:CGRectMake(405, 510, 20, 20)],
[NSValue valueWithCGRect:CGRectMake(420, 500, 20, 20)],
[NSValue valueWithCGRect:CGRectMake(435, 510, 20, 20)],
nil];
NSArray *arrayFullOfRotateTransforms = [NSArray arrayWithObjects:
[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/4)],
[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/2)],
[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/1)],
[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/4)],
[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/2)],
[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/1)],
[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/3)],
[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/5)],
[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/6)],
[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_PI/1)],
nil];
NSDictionary *drawDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
arrayFullOfCGRects, @"frames",
arrayFullOfRotateTransforms, @"rotations",
nil];
for(NSInteger charIndex=0; charIndex<stringToCurve.length; charIndex++){
//Create a label and set it's frame coordinates
UILabel *labelToDraw = [[UILabel alloc] initWithFrame:[[[drawDictionary objectForKey:@"frames"] objectAtIndex:charIndex] CGRectValue]];
//Rotate the letter with the transform stored in the array
labelToDraw.transform = [[[drawDictionary objectForKey:@"rotations"] objectAtIndex:charIndex] CGAffineTransformValue];
//Set the label text to the letter at the correct index
labelToDraw.text = [NSString stringWithFormat:@"%C", [stringToCurve characterAtIndex:charIndex]];
//Make sure the background colour is transparent
labelToDraw.backgroundColor = [UIColor clearColor];
//Draw it to the view controller view
[self.view addSubview:labelToDraw];
}
UIGraphicsEndImageContext();
肯定有更好的方法吗?一个drawTextOnPath:someCGPath方法,我不知道/找不到?