layoutNextLine() is an iterator-style API that lays out one line at a time, accepting a different maxWidth for each line. Use it when the available width changes as you descend through the paragraph — for example, when text flows around a floated image or fills a multi-column editorial layout.
Signature
Parameters
The handle returned by
prepareWithSegments().Cursor marking where to start the next line. For the first line, pass
{ segmentIndex: 0, graphemeIndex: 0 }. For subsequent lines, pass the previous line’s end cursor.Maximum width for this specific line in pixels. Can differ on every call — this is the key capability that distinguishes
layoutNextLine() from layoutWithLines().Returns
A
LayoutLine object for the current line, or null when the paragraph is exhausted (no more text to lay out).Pattern: advance the cursor
The core loop pattern is:- Call
layoutNextLine(prepared, cursor, width)with the current cursor and the width available for this line. - If the return value is
null, the paragraph is done — exit the loop. - Render the line.
- Set
cursor = line.endand advance youryposition. - Repeat, computing a new
widthfor the next line.
When to use
| Scenario | Recommended API |
|---|---|
| Fixed width, need height only | layout() |
| Fixed width, need line text | layoutWithLines() |
| Geometry-only, probe multiple widths | walkLineRanges() |
| Variable width per line | layoutNextLine() |
layoutNextLine() for:
- Text flowing around floated images or other obstacles.
- Multi-column editorial layouts where column width changes at a fold.
- Any custom layout engine where the available line width is computed dynamically.