Rotating NSString in Swift

≡放荡痞女 提交于 2019-12-25 10:23:06

问题


I'm trying to do something like the attached image.

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)

回答1:


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.


来源:https://stackoverflow.com/questions/30089756/rotating-nsstring-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!