在Swift中旋转NSString [英] Rotating NSString in Swift

查看:68
本文介绍了在Swift中旋转NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做类似附件的图片。

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


在drawRect中旋转NSString

使用NSString drawInRect绘制旋转文本

迅速使objC麻烦。我可以轻松画出箭头,但是让NSString旋转然后平移是有问题的。它确实会产生怪异的现象,例如画出屏幕,错误的角度等。

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)


推荐答案

思考关于您要执行的操作:在距圆心一定距离处绘制一个字符串,并围绕圆心旋转。因此,将圆心视为原点会很方便。我将执行以下操作:

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. 平移CTM,使原点位于圆心。

  2. 旋转到正确的角度。

  3. {d,0} (其中 d 是距文本应开始的中心的距离。

  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.

这篇关于在Swift中旋转NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆