For the application I am creating, I drew 2 circles. One is a little bigger than the other. I want to draw text between these lines, for the circular menu that I create.
I read most about the fact that you are bending the text that you need to divide into text in characters, and draw each character on it with the correct angle (by turning the context you are drawing onto).
I just can't get my head around how to get the right angles and positions for my characters.
I included a screenshot of what the menu looks like at the moment. Only the texts that I added are loaded from the image into the UIImageView.

Hope someone can get me some starting points on how I can draw text in a white circle at specific points.
EDIT: Ok, now I'm here:

I execute the following code:
- (UIImage*) createMenuRingWithFrame:(CGRect)frame { CGRect imageSize = CGRectMake(0,0,300,300); float perSectionDegrees = 360 / [sections count]; float totalRotation = 90; char* fontName = (char*)[self.menuItemsFont.fontName cStringUsingEncoding:NSASCIIStringEncoding]; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, imageSize.size.width, imageSize.size.height, 8, 4 * imageSize.size.width, colorSpace, kCGImageAlphaPremultipliedFirst); CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextSelectFont(context, fontName, 18, kCGEncodingMacRoman); CGContextSetRGBFillColor(context, 0, 0, 0, 1); CGPoint centerPoint = CGPointMake(imageSize.size.width / 2, imageSize.size.height / 2); double radius = (frame.size.width / 2); CGContextStrokeEllipseInRect(context, CGRectMake(centerPoint.x - (frame.size.width / 2), centerPoint.y - (frame.size.height / 2), frame.size.width, frame.size.height)); for (int index = 0; index < [sections count]; index++) { NSString* menuItemText = [sections objectAtIndex:index]; CGSize textSize = [menuItemText sizeWithFont:self.menuItemsFont]; char* menuItemTextChar = (char*)[menuItemText cStringUsingEncoding:NSASCIIStringEncoding]; float x = centerPoint.x + radius * cos(degreesToRadians(totalRotation)); float y = centerPoint.y + radius * sin(degreesToRadians(totalRotation)); CGContextSaveGState(context); CGContextTranslateCTM(context, x, y); CGContextRotateCTM(context, degreesToRadians(totalRotation - 90)); CGContextShowTextAtPoint(context, 0 - (textSize.width / 2), 0 - (textSize.height / 2), menuItemTextChar, strlen(menuItemTextChar)); CGContextRestoreGState(context); totalRotation += perSectionDegrees; } CGImageRef contextImage = CGBitmapContextCreateImage(context); CGContextRelease(context); CGColorSpaceRelease(colorSpace); return [UIImage imageWithCGImage:contextImage]; }
These are the variables that I use there:
NSArray* sections = [[NSArray alloc] initWithObjects:@"settings", @"test", @"stats", @"nog iets", @"woei", @"woei2", nil]; self.menuItemsFont = [UIFont fontWithName:@"VAGRounded-Bold" size:18];
Word rotation seems correct, and placement. Now I need to somehow figure out what rotation the letters should be (and their coordinates). I could help with that.
Edit: Fixed! Check out the following code!
- (void) drawStringAtContext:(CGContextRef) context string:(NSString*) text atAngle:(float) angle withRadius:(float) radius { CGSize textSize = [text sizeWithFont:self.menuItemsFont]; float perimeter = 2 * M_PI * radius; float textAngle = textSize.width / perimeter * 2 * M_PI; angle += textAngle / 2; for (int index = 0; index < [text length]; index++) { NSRange range = {index, 1}; NSString* letter = [text substringWithRange:range]; char* c = (char*)[letter cStringUsingEncoding:NSASCIIStringEncoding]; CGSize charSize = [letter sizeWithFont:self.menuItemsFont]; NSLog(@"Char %@ with size: %fx %f", letter, charSize.width, charSize.height); float x = radius * cos(angle); float y = radius * sin(angle); float letterAngle = (charSize.width / perimeter * -2 * M_PI); CGContextSaveGState(context); CGContextTranslateCTM(context, x, y); CGContextRotateCTM(context, (angle - 0.5 * M_PI)); CGContextShowTextAtPoint(context, 0, 0, c, strlen(c)); CGContextRestoreGState(context); angle += letterAngle; } } - (UIImage*) createMenuRingWithFrame:(CGRect)frame { CGPoint centerPoint = CGPointMake(frame.size.width / 2, frame.size.height / 2); char* fontName = (char*)[self.menuItemsFont.fontName cStringUsingEncoding:NSASCIIStringEncoding]; CGFloat* ringColorComponents = (float*)CGColorGetComponents(ringColor.CGColor); CGFloat* textColorComponents = (float*)CGColorGetComponents(textColor.CGColor); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, frame.size.width, frame.size.height, 8, 4 * frame.size.width, colorSpace, kCGImageAlphaPremultipliedFirst); CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextSelectFont(context, fontName, 18, kCGEncodingMacRoman); CGContextSetRGBStrokeColor(context, ringColorComponents[0], ringColorComponents[1], ringColorComponents[2], ringAlpha); CGContextSetLineWidth(context, ringWidth); CGContextStrokeEllipseInRect(context, CGRectMake(ringWidth, ringWidth, frame.size.width - (ringWidth * 2), frame.size.height - (ringWidth * 2))); CGContextSetRGBFillColor(context, textColorComponents[0], textColorComponents[1], textColorComponents[2], textAlpha); CGContextSaveGState(context); CGContextTranslateCTM(context, centerPoint.x, centerPoint.y); float angleStep = 2 * M_PI / [sections count]; float angle = degreesToRadians(90); textRadius = textRadius - 12; for (NSString* text in sections) { [self drawStringAtContext:context string:text atAngle:angle withRadius:textRadius]; angle -= angleStep; } CGContextRestoreGState(context); CGImageRef contextImage = CGBitmapContextCreateImage(context); CGContextRelease(context); CGColorSpaceRelease(colorSpace); [self saveImage:[UIImage imageWithCGImage:contextImage] withName:@"test.png"]; return [UIImage imageWithCGImage:contextImage]; }