Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

[iPhone] Rotate NSString

Hey Guys,

This seems like a pretty common question but I can't seem to find it posted anywhere (on this forum or elsewhere).

My question is: is it possible to do a rotation on a "NSString drawInRect" call.

I like using the "NSString drawInRect" call since it's able to take long strings and wrap them into a several lines. But I was hoping I could rotate this box a little.

I have looked on google for a solution for rotating text and I found this:
------------------------------------
CGContextSelectFont (context, "Helvetica", 60, kCGEncodingMacRoman);
CGContextSetCharacterSpacing (context, 10);
CGContextSetTextDrawingMode (context, kCGTextFill);
CGContextSetGrayFillColor(context, 0.0, 1.0);
CGAffineTransform myTextTransform = CGAffineTransformRotate(CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f ),(-1));
CGContextSetTextMatrix (context, myTextTransform);
CGContextShowTextAtPoint (context, 0, 0, "Quartz 2D", 9);
------------------------------------
The code works fine for me, but it doesn't have that line wrapping feature that I get with the "NSString drawInRect" call.

Is there anyway to get my lines of text to rotate?

Thanks for the help.

Cheers,
Mark

Mac OS X (10.5.5), iPhone

Posted on Nov 5, 2008 9:51 PM

Reply
2 replies

Nov 6, 2008 3:32 PM in response to krammark32

Figured it out - turns out it was a pretty lame question.

Oh well, here's my solution for anyone else that's interested.

----------------------------------
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(M_PI/4);
CGContextSaveGState(context);
CGContextConcatCTM(context, rotateTransform);
[currentDisplayString drawInRect:CGRectMake(0, 0, 200, 100) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UIBaselineAdjustmentAlignBaselines];
CGContextRestoreGState(context);
----------------------------------

[iPhone] Rotate NSString

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.