views:

141

answers:

1

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 17-1. A flip transform involves inverting the y-axis and translating the origin point to the bottom of the screen. Listing 17-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 17-1

Why? This seems totally whacky.

+1  A: 

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.

Brad Larson