0

I'm trying to do something like the attached image. I am the one who knocks.

I thought this page would lead to the answer

http://www.informit.com/articles/article.aspx?p=2149190&seqNum=13

or

Rotating a NSString in drawRect

Drawing rotated text with NSString drawInRect

but converting objC to swift is troublesome. I can easily draw my arrows but getting the NSString to rotate then translate is problematic. It does weird things like drawing off screen, wrong angles, etc.

I've tried this and many variations

  CGContextSaveGState(context)
  CGContextRotateCTM(context, dTheta[i])
  CGContextTranslateCTM(context, -s.sizeWithAttributes(attributes).width / 2.0, -radiusY )
  s.drawAtPoint(CGPoint(x: x + xOffset*0.0 , y: y + yOffset*0.0 ), withAttributes: attributes)
  //s.drawAtPoint(CGPointMake(0, 0), withAttributes: attributes)
  CGContextRestoreGState(context)
Community
  • 1
  • 1

1 Answers1

2

Think about what you want to do: draw a string some distance from the center of the circle, rotated about the center of the circle. So, it'd be handy to think of the center of the circle as the origin. I'd do the following:

  1. Translate the CTM so that the origin is at the center of the circle.
  2. Rotate to the correct angle.
  3. Draw the string at {d, 0} (where d is the distance from the center where the text should start.
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Thanks. My radial lines are not showing now. I used a separate context, did `CGContextSaveGState(contextLines)` and finally `CGContextRestoreGState(contextLines)` but no joy. – μολὼν.λαβέ May 07 '15 at 02:41
  • 1
    I'd draw the lines at the same time as the text. Just remember that you've already rotated the CTM, so you'll draw the line along the `x` axis, i.e. from the origin `{0,0}` to the point `{d,0}`. – Caleb May 07 '15 at 02:41
  • Thanks, almost there. I'm finding that for some reason the attribute colors are not being respected. ` s.drawAtPoint(CGPoint(x: radius , y: 0 ), withAttributes: [NSForegroundColorAttributeName: UIColor.yellowColor() ])` the lines should be yellow but they are black?? – μολὼν.λαβέ May 07 '15 at 03:15