图片:http://i.stack.imgur.com/pbzar.png
我希望文字在90-270度(文字“Aroma 7”到“Aroma 17”),旋转180度。
我的代码:
for (int i=0; i<24; i++) {
CGContextSaveGState(context);
CGContextRef context = UIGraphicsGetCurrentContext();
NSString *str = [NSString stringWithFormat:@"Aroma %d", i];
CGContextTranslateCTM(context, radius, radius);
CGContextRotateCTM(context, i * 15 * M_PI/180.0);
[[UIColor whiteColor] set];
CGContextTranslateCTM(context, - (radius), -(radius));
CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12.0]
constrainedToSize:rect.size
lineBreakMode:(NSLineBreakByWordWrapping)];
[str drawAtPoint:CGPointMake(((radius * 2) - 10) - size.width, radius) withFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
CGContextRestoreGState(context);
}
感谢您的帮助!
答案 0 :(得分:5)
这应该产生想要的输出:
for (int i=0; i<24; i++) {
CGContextSaveGState(context);
CGContextRef context = UIGraphicsGetCurrentContext();
NSString *str = [NSString stringWithFormat:@"Aroma %d", i];
CGContextTranslateCTM(context, radius, radius);
CGContextRotateCTM(context, i * 15 * M_PI/180.0);
[[UIColor whiteColor] set];
CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12.0]
constrainedToSize:rect.size
lineBreakMode:(NSLineBreakByWordWrapping)];
CGContextTranslateCTM(context, radius-10-size.width/2, size.height/2); // (1)
if (i >= 7 && i <= 17)
CGContextRotateCTM(context, M_PI); // (2)
[str drawAtPoint:CGPointMake(-size.width/2, -size.height/2) withFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
CGContextRestoreGState(context);
}
我们的想法是将坐标系的原点移动到要绘制文本的矩形的中心(参见(1)
)。然后您可以将文本旋转180度(参见(2)
)。
这是我测试程序的输出: