为什么 iPhone 上需要 Quartz 2D 文本翻转变换

发布于 2024-09-15 02:14:38 字数 458 浏览 2 评论 0原文

来自苹果文档:

在 iOS 中,您必须对当前图形上下文应用翻转变换,以便使文本定向,如图 16-1 所示。 [翻转变换]涉及反转 y 轴并将原点平移到屏幕底部。清单 16-2 展示了如何在 iOS 视图的 drawRect: 方法中应用此类转换。然后,该方法调用清单 16-1 中相同的 MyDrawText 方法。

为什么?这看起来很奇怪。

From the Apple docs:

In iOS, you must apply a flip transform to the current graphics context in order for the text to be oriented as shown in Figure 16-1. [A flip transform] involves inverting the y-axis and translating the origin point to the bottom of the screen. Listing 16-2 shows you how to apply such transformations in the drawRect: method of an iOS view. This method then calls the same MyDrawText method from Listing 16-1.

Why? This seems totally whacky.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒江雪… 2024-09-22 02:14:38

正如我在这个答案中展开的< /a>,Quartz 2D坐标系使用左下角为(0,0),与Mac上的窗口坐标系相同。 iPhone 使用左上角作为其视图布局的 (0,0),因此支持 UIView 的图层的坐标系绕 Y 轴翻转。

如果您使用 NSString UIKit 扩展将文本绘制到 UIView 的支持层中,它将正确定向,因为考虑了这种翻转。但是,如果您使用较低级别的 Quartz 文本绘图,则需要首先围绕 Y 轴翻转坐标系(以便 0,0 再次位于左下角)以正确定向事物。

正常的 Core Graphics 上下文(用于绘制图像或 PDF 以进行存储或显示)不会反转,因此情况正好相反。普通的 Quartz 文本可以很好地渲染,但是使用 NSString UIKit 扩展绘制的内容需要首先反转坐标系。这给开发人员带来了很多困惑,他们的图像和文本在绘制到屏幕上时看起来是正确的,但在保存到磁盘时却是颠倒的。

至于为什么这样做,大家都在猜测。这两种坐标系在某些情况下都有其优点。不过,这是一件很容易纠正的事情。

As I expand upon in this answer, the Quartz 2D coordinate system uses the lower left corner as (0,0), which is the same as the window coordinate system on the Mac. The iPhone uses the upper left corner as (0,0) for its view layout, so the layers that back UIViews have their coordinate system flipped about the Y axis.

If you use the NSString UIKit extensions to draw text into the backing layer for a UIView, it will be oriented correctly because this flipping is taken into account. However, if you use the lower level Quartz text drawing, you will need to flip the coordinate system about the Y axis first (so that 0,0 is once again the lower left) to orient things properly.

Normal Core Graphics contexts (used for drawing images or PDFs for storage or display) are not inverted, so the opposite is true. Normal Quartz text will render fine, but stuff drawn using the NSString UIKit extensions will need to have the coordinate system inverted first. This causes a lot of confusion among developers whose images and text look right when drawn to the screen, but end up upside-down when saved to disk.

As for why this was done, it's anybody's guess. Both coordinate systems have their advantages in certain circumstances. It's an easy thing to correct for, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文