Easiest way to display a rotated NSString?

Is there an easy way to rotate and display an NSString?

I'm currently using the following to draw on screen but would like to rotate (either +90 or -90 degrees):


UIFont *smallFont = [UIFont systemFontOfSize:8];
CGPoint textPoint = CGPointMake(10, 10);
[[NSString stringWithString:@"MY STRING"] drawAtPoint:textPoint withFont:smallFont];


I know another option would be to use UILabels and CATransform3D but I can't seem to clear teh screen when calling:


[self setNeedsDisplay];


therefore it just gets messy with text displaying over text. I was using the following:


UIFont *smallFont = [UIFont systemFontOfSize:8];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 120, 50)];
myLabel.font = smallFont;
myLabel.text = [NSString stringWithString:@"MY STRING"];
myLabel.textColor = [UIColor blackColor];
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textAlignment = UITextAlignmentCenter;
CATransform3D landscapeTransform = CATransform3DIdentity;
landscapeTransform = CATransform3DRotate(landscapeTransform, DegreesToRadians(-90), 0, 0, 1);
myLabel.layer.transform = landscapeTransform;
[self addSubview:myLabel];
[myLabel release];


Thanks

iMac, Mac OS X (10.5.4), N/A

Posted on Aug 14, 2008 11:01 AM

Reply
2 replies

Aug 15, 2008 1:43 AM in response to RJShearman

I've managed to get text displayed using CGContextShowTextAtPoint but the problem is the text is upside down.

I can fix the upside down text by doing the following:

CGContextSelectFont (context, "Helvetica", 60, kCGEncodingMacRoman);
CGContextSetCharacterSpacing (context, 10);
CGContextSetTextDrawingMode (context, kCGTextFill);
CGContextSetGrayFillColor(context, 0.0, 1.0);
CGContextSetTextMatrix(context, CGAffineTransformScale( CGAffineTransformIdentity, 1.f, -1.f ) ); // flip
CGContextShowTextAtPoint (context, 100, 200, "Quartz 2D", 9);


I can rotate the text by doing the following:

CGFloat DegreesToRadiansText(CGFloat degrees) {return degrees * M_PI / 180;};
CGContextSelectFont (context, "Helvetica", 60, kCGEncodingMacRoman);
CGContextSetCharacterSpacing (context, 10);
CGContextSetTextDrawingMode (context, kCGTextFill);
CGContextSetGrayFillColor(context, 0.0, 1.0);
CGAffineTransform myTextTransform = CGAffineTransformMakeRotation (DegreesToRadiansText (-90)); // rotate
CGContextSetTextMatrix (context, myTextTransform); // rotate
CGContextShowTextAtPoint (context, 100, 200, "Quartz 2D", 9);


But I can't seem to be able to flip the text AND rotate it before displaying on screen.

Can anyone see what i'm missing?

Aug 15, 2008 4:24 AM in response to RJShearman

Working.

If interested:

CGFloat DegreesToRadiansText(CGFloat degrees) {return degrees * M_PI / 180;};

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 ),(DegreesToRadiansText (-90));
CGContextSetTextMatrix (context, myTextTransform);
CGContextShowTextAtPoint (context, 100, 200, "Quartz 2D", 9);

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Easiest way to display a rotated NSString?

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