Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Quartz 2D text flip transform required on iPhone

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.

like image 453
codecowboy Avatar asked Aug 15 '10 05:08

codecowboy


1 Answers

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.

like image 99
Brad Larson Avatar answered Oct 20 '22 04:10

Brad Larson