9.11 · Transform order, fill rules, text measurement, text containers and bitmaps
Drawing and text bugs are usually arithmetic bugs: an order of operations, a winding rule, a rounding, a scale.
Mental model. Each of these has a precise rule that you can print. Learn the rule once and the picture stops being a surprise.
Geometry rules
Affine transform order and path fill rules.
- In which order do CGAffineTransform operations apply, and what do scaledBy, rotated and translatedBy do differently from concatenating?
A CGAffineTransform is a 2D matrix; concatenating reads left to right as “first this, then that”: T.concatenating(S) translates by 100 then scales by 2, so the… - How do the non-zero and even-odd fill rules decide what is inside a path, and what does contains(_:) ignore?
For a path with overlapping or nested sub-paths, “inside” needs a rule. Non-zero winding (the default) draws a ray from the point and adds one for each path…
Text and bitmaps
Measuring text, shaping its container, and what a bitmap really costs.
- How do you measure text correctly: font metrics, boundingRect options and rounding, and what paragraph styles add?
A font has an ascender, a descender (negative) and leading (extra line gap); lineHeight is their sum (ascender − descender + leading), which the check confirms… - How do exclusion paths, line limits and truncation work in an NSTextContainer, and what do UILabel’s own limits do?
In TextKit the container decides where lines may go. An NSTextContainer has a size (the wrapping width), exclusionPaths (regions the text must flow around, in… - What does an image cost in pixels, what does opaque change in a UIGraphicsImageRenderer, and what do resizing and rendering modes do?
A UIImage has a size in points and a scale; the bitmap behind it has size × scale pixels, 4 bytes each in the usual 32-bit format, and the rows may be padded.…
More in Layout, rendering & animation
- 9.1 · Auto Layout, intrinsic size, and when layoutSubviews and draw run
- 9.2 · From layout to the GPU, and the two layer trees
- 9.3 · Offscreen passes, rasterization, blending, shadows and masks
- 9.4 · Implicit and explicit animation, UIView animation, and animators
- 9.5 · Core Graphics, UIBezierPath and draw(_:)
- 9.6 · TextKit 1 and 2, and Text in SwiftUI
- 9.7 · Frame budgets, ProMotion, hitches and hangs
- 9.8 · Margins, stack views, guides, constraint ownership, the layout cycle and measuring
- 9.9 · Coordinate conversion, 3D transforms and layer contents
- 9.10 · How UIView animation really works, springs, timing and display links
- 9.12 · Rectangle containment and moving a point between coordinate spaces