First day on a new job, I've been asked to help out on an iPad app. They have asked me to draw an NSString along a curved path (the curve is sort of like an S shape).
Not really sure how to do this without looping through the letters of the string and drawing each letter individually after rotating and moving it's frame. Is there a more efficient way of doing this?
Here's what I have so far:
-(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();
There has to be a better way surely? A drawTextOnPath:someCGPath method that I don't know about/can't find?