CGContextShowTextAtPoint 渲染颠倒
我试图通过CGContextShowTextAtPoint()
通过Quartz
在NSView
上绘制一些文本。这工作得很好,直到我覆盖 (BOOL)isFlipped
以在我的 NSView
子类中返回 YES
,以便将原点定位在左上角绘画。文本绘制在预期区域,但字母全部倒置。我还尝试了(至少理论上)相当于翻转 CGContext 并按上下文高度进行翻译的操作。
ex
// drawRect:
CGContextScaleCTM(theContext, 1, -1);
CGContextTranslateCTM(theContext, 0, -dirtyRect.size.height);
这会产生相同的结果。
针对类似问题的许多建议都指向修改文本矩阵。我已将文本矩阵设置为单位矩阵,对其执行额外的反转,并分别完成这两项操作。所有这些解决方案都会导致更奇怪的文本渲染(通常只显示一个片段。)
我看到的另一个建议是简单地避开此函数,而采用其他绘制文本的方法(例如 NSString
的绘图方法。)但是,这主要是在 C++ / C 中完成的,如果可能的话,我希望保持在这些水平。
非常感谢任何建议,如果需要,我很乐意发布更多代码。
谢谢, 山姆
I am trying to draw some text via Quartz
onto an NSView
via CGContextShowTextAtPoint()
. This worked well until I overrode (BOOL)isFlipped
to return YES
in my NSView
subclass in order to position the origin in the upper-left for drawing. The text draws in the expected area but the letters are all inverted. I also tried the (theoretically, at least) equivalent of flipping my CGContext and translating by the context's height.
e.x.
// drawRect:
CGContextScaleCTM(theContext, 1, -1);
CGContextTranslateCTM(theContext, 0, -dirtyRect.size.height);
This yields the same result.
Many suggestions to similar problems have pointed to modifying the text matrix. I've set the text matrix to the identity matrix, performed an additional inversion on it, and done both, respectively. All these solutions have lead to even stranger rendering of the text (often just a fragment shows up.)
Another suggestion I saw was to simply steer clear of this function in favor of other means of drawing text (e.x. NSString
's drawing methods.) However, this is being done amongst mostly C++ / C and I'd like to stay at those levels if possible.
Any suggestions are much appreciated and I'd be happy to post more code if needed.
Thanks,
Sam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此问题已在此处得到解答。
基本上,这是因为 iOS 核心图形上的坐标系被翻转(
x:0, y:0
在左上角),与 Mac 上的坐标系(其中x:0, y: 0
位于左下角)。解决方案是设置文本转换矩阵,如下所示:This question has been answered here.
Basically it's because the coordinate system on iOS core graphics is fliped (
x:0, y:0
in the top left) opposed to the one on the Mac (wherex:0, y:0
is bottom left). The solution for this is setting the text transform matrix like this:您需要使用视图的边界而不是 dirtyRect 并在缩放之前执行平移:
You need to use the view's bounds rather than the dirtyRect and perform the translation before the scale:
事实证明,答案是修改文本矩阵。显示而不是文本的奇怪“片段”是因为替换“默认”文本矩阵时字体大小(通过 CGContextSelectFont() 设置)太小。由于某种原因,初始矩阵进行了大规模变换,因此当矩阵未修改时,较小的文本尺寸看起来很好;然而,当用逆标度 (1, -1) 或单位矩阵替换时,它们会变得小得难以阅读。
Turns out the answer was to modify the text matrix. The weird "fragments" that were showing up instead of the text was because the font size (set via CGContextSelectFont()) was too small when the "default" text matrix was replaced. The initial matrix had, for some reason, a large scale transform so smaller text sizes looked fine when the matrix was unmodified; when replaced with a inverse scale (1, -1) or an identity matrix, however, they would become unreadably small.