How to make equal space between texts in the circular menu?
Asked Answered
S

0

0

I am creating a circular slider menu, I am using coregraphics to draw the control UI, Here I am trying to draw words on the circumference of a circle. But i cant able to make the spaces equally.

Here is my code and output. Please help me to give equal spaces between each word.

In drawRect

float angleStep = 2 * M_PI / [menuArray count];
float angle = degreesToRadians(90);
textRadius = textRadius - 12;

for (NSString* text in`enter code here` titleArray)
{
    [self drawStringAtContext:ctx string:text atAngle:angle withRadius:textRadius];
    angle -= angleStep;
}

//////////

- (void) drawStringAtContext:(CGContextRef) context string:(NSString*) text atAngle:(float)angle withRadius:(float) radis

{
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:11.0]];

float perimeter = 2 * M_PI * radis;
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:[UIFont systemFontOfSize:11.0]];

    float x = radis * cos(angle);
    float y = radis * 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;
}
}

Output

enter image description here

Stationary answered 23/4, 2015 at 9:50 Comment(3)
Since you get the angle at the end of each letter, I'd suggest that you add each word into a UIView, keep its "whole angle", then dispose them around your circle (angleStep = 2*PI/sum(all_word "whole angle"). Then rotate each view at its correct angle.Slip
I am new to coregraphics, also i could not able to understand your logic, even i tried github.com/juggleware/CoreTextArcView_iOS but it seems like the arch is not getting properly. Can you elaborate your logic?Stationary
Try appending all your strings into one: wholeString. And redefine angleStep = wholeTextSize.width / perimeter * 2 * M_PI;Slip

© 2022 - 2024 — McMap. All rights reserved.