【问题标题】:rotate text in customview [duplicate]在customview中旋转文本[重复]
【发布时间】:2013-03-21 22:41:13
【问题描述】:

我可以使用drawAtPoint 方法在CustomView 中绘制NSString

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

如何在同一位置将文本旋转 90 度?到目前为止,我发现并没有真正在定义的位置旋转文本。

【问题讨论】:

  • 食梦​​者您好,感谢您的回答。我查了一下,我不相信,因为 Mac 开发人员库声明了这一点: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. 。所以,Aleph7 是在正确的轨道上,但我需要找出以某种方式使其适应 osx。
  • 你的帮助根本没有帮助。谢谢。

标签: objective-c nsstring rotatetransform


【解决方案1】:

只需对图形上下文应用旋转:

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

// ...
[text drawAtPoint:p withAttributes:xlabelSet]; //draw text at position

【讨论】:

  • 您好 Aleph7,感谢您的回答。它看起来像它的iOS。 osx 也有类似的东西吗?
  • 使用UIGraphicsGetCurrentContext() 会产生错误implicit declaration of function 'UIGraphicsGetCurrentContext' is invalid in C99。这到底是什么意思?
  • 将第一行替换为CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
  • 您好 Aleph7,感谢您一直关注这里。这些更改确实会旋转文本,但我认为我仍然需要做更多的研究。这似乎并不像我一开始想的那么容易。无论如何,谢谢。
猜你喜欢
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多