0

I'm able to draw a NSString in a CustomView using the drawAtPoint method:

NSString *text = @"text";
NSSize textSize = [text sizeWithAttributes:xlabelSet]; //receive the text size
NSPoint p = NSMakePoint(length - textSize.width/2 , wcszero_y - 20); //define text position considering the text center
[text drawAtPoint:p withAttributes:xlabelSet]; //draw text at position

How do I rotate the text by 90 degree at the same position? What I found so far wasn't really rotating the text at the defined position.

JFS
  • 2,992
  • 3
  • 37
  • 48
  • Hello Dream Eater, Thanks for the answer. I looked it up and I'm not convinced since the Mac Developer Library states this: `If you want only to rotate an object to be drawn, it is not necessary to construct an affine transform to do so. The most direct way to rotate your drawing is by calling the function CGContextRotateCTM. `. So, Aleph7 is on the right track but I need to find out to adapt this to osx somehow. – JFS Mar 22 '13 at 11:04
  • your help doesn't help at all. Thanks. – JFS Mar 22 '13 at 22:04

1 Answers1

2

Just apply a rotation to the graphics context:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRotateCTM(context, M_PI/2);

// ...
[text drawAtPoint:p withAttributes:xlabelSet]; //draw text at position
Alejandro
  • 3,726
  • 1
  • 23
  • 29
  • Hello Aleph7, thank you for the answer. It looks like its for iOS. Is there something similar for osx as well? – JFS Mar 21 '13 at 22:58
  • using `UIGraphicsGetCurrentContext()` gives an error `implicit declaration of function 'UIGraphicsGetCurrentContext' is invalid in C99`. What the heck does that mean? – JFS Mar 21 '13 at 23:43
  • 1
    replace the first line with `CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];` – Alejandro Mar 22 '13 at 17:51
  • Hello Aleph7, thanks for keeping up here. The changes do actually rotate the text but I think I still need to do some more research. It seems to be not as easy as I thought in the first place. Thanks anyhow. – JFS Mar 22 '13 at 21:55