Contents

Text.LayoutKey.AnchoredLayout

The layout of one text view, together with an anchor for the position of that view.

Declaration

struct AnchoredLayout

Overview

Text.LayoutKey collects one of these values for every text view in the subtree you query. Read layout to inspect the lines, runs, and glyphs the text produced, and resolve origin in a GeometryProxy to place something of your own next to the text:

ZStack {
    Text("Hello, world")
}
.overlayPreferenceValue(Text.LayoutKey.self) { layouts in
    GeometryReader { proxy in
        ForEach(0..<layouts.count, id: \.self) { index in
            Underline(layout: layouts[index].layout)
                .position(proxy[layouts[index].origin])
        }
    }
}

The anchor matters because a text view reports its layout in its own coordinate space. Resolving the anchor converts that origin into the space of the view reading the preference.

Topics

Instance Properties