TextLayoutResult

Class

Common
class TextLayoutResult
constructor(
    /** The parameters used for computing this text layout result. */
    val layoutInput: TextLayoutInput,

    /**
     * The multi paragraph object.
     *
     * This is the result of the text layout computation.
     */
    val multiParagraph: MultiParagraph,

    /** The amount of space required to paint this text in Int. */
    val size: IntSize,
)

The data class which holds text layout result.

Properties

Common
val firstBaseline: Float

The distance from the top to the alphabetic baseline of the first line.

Common
val lastBaseline: Float

The distance from the top to the alphabetic baseline of the last line.

Common
val didOverflowHeight: Boolean

Returns true if the text is too tall and couldn't fit with given height.

Common
val didOverflowWidth: Boolean

Returns true if the text is too wide and couldn't fit with given width.

Common
val hasVisualOverflow: Boolean

Returns true if either vertical overflow or horizontal overflow happens.

Common
val placeholderRects: List<Rect?>

Returns a list of bounding boxes that is reserved for TextLayoutInput.placeholders. Each Rect in this list corresponds to the Placeholder passed to TextLayoutInput.placeholders and it will have the height and width specified in the Placeholder. It's guaranteed that TextLayoutInput.placeholders and TextLayoutResult.placeholderRects will have same length and order.

Common
val lineCount: Int

Returns a number of lines of this text layout

Functions

fun getLineStart(lineIndex: Int): Int

Returns the start offset of the given line, inclusive.

The start offset represents a position in text before the first character in the given line. For example, getLineStart(1) will return 4 for the text below

┌────┐
│abcd│
│efg │
└────┘

Parameters

lineIndexthe line number

Returns

the start offset of the line
fun getLineEnd(lineIndex: Int, visibleEnd: Boolean = false): Int

Returns the end offset of the given line.

The end offset represents a position in text after the last character in the given line. For example, getLineEnd(0) will return 4 for the text below

┌────┐
│abcd│
│efg │
└────┘

Characters being ellipsized are treated as invisible characters. So that if visibleEnd is false, it will return line end including the ellipsized characters and vice versa.

Parameters

lineIndexthe line number
visibleEndif true, the returned line end will not count trailing whitespaces or linefeed characters. Otherwise, this function will return the logical line end. By default it's false.

Returns

an exclusive end offset of the line.
fun isLineEllipsized(lineIndex: Int): Boolean

Returns true if the given line is ellipsized, otherwise returns false.

Parameters

lineIndexa 0 based line index

Returns

true if the given line is ellipsized, otherwise false
fun getLineTop(lineIndex: Int): Float

Returns the top y coordinate of the given line.

Parameters

lineIndexthe line number

Returns

the line top y coordinate
fun getLineBaseline(lineIndex: Int): Float

Returns the distance in pixels from the top of the text layout to the alphabetic baseline of the line at index lineIndex.

fun getLineBottom(lineIndex: Int): Float

Returns the bottom y coordinate of the given line.

Parameters

lineIndexthe line number

Returns

the line bottom y coordinate
fun getLineLeft(lineIndex: Int): Float

Returns the left x coordinate of the given line.

Parameters

lineIndexthe line number

Returns

the line left x coordinate
fun getLineRight(lineIndex: Int): Float

Returns the right x coordinate of the given line.

Parameters

lineIndexthe line number

Returns

the line right x coordinate
fun getLineForOffset(offset: Int): Int

Returns the line number on which the specified text offset appears.

If you ask for a position before 0, you get 0; if you ask for a position beyond the end of the text, you get the last line.

Parameters

offseta character offset

Returns

the 0 origin line number.
fun getLineForVerticalPosition(vertical: Float): Int

Returns line number closest to the given graphical vertical position.

If you ask for a vertical position before 0, you get 0; if you ask for a vertical position beyond the last line, you get the last line.

Parameters

verticalthe vertical position

Returns

the 0 origin line number.
fun getHorizontalPosition(offset: Int, usePrimaryDirection: Boolean): Float

Get the horizontal position for the specified text offset.

Returns the relative distance from the text starting offset. For example, if the paragraph direction is Left-to-Right, this function returns positive value as a distance from the left-most edge. If the paragraph direction is Right-to-Left, this function returns negative value as a distance from the right-most edge.

usePrimaryDirection argument is taken into account only when the offset is in the BiDi directional transition point. usePrimaryDirection is true means use the primary direction run's coordinate, and use the secondary direction's run's coordinate if false.

Parameters

offseta character offset
usePrimaryDirectiontrue for using the primary run's coordinate if the given offset is in the BiDi directional transition point.

Returns

the relative distance from the text starting edge.
fun getParagraphDirection(offset: Int): ResolvedTextDirection

Get the text direction of the paragraph containing the given offset.

Parameters

offseta character offset

Returns

the paragraph direction
fun getBidiRunDirection(offset: Int): ResolvedTextDirection

Get the text direction of the resolved BiDi run that the character at the given offset associated with.

Parameters

offseta character offset

Returns

the direction of the BiDi run of the given character offset.
fun getOffsetForPosition(position: Offset): Int

Returns the character offset closest to the given graphical position.

Parameters

positiona graphical position in this text layout

Returns

a character offset that is closest to the given graphical position.
fun getBoundingBox(offset: Int): Rect

Returns the bounding box of the character for given character offset.

Parameters

offseta character offset

Returns

a bounding box for the character in pixels.
fun getWordBoundary(offset: Int): TextRange

Returns the text range of the word at the given character offset.

Characters not part of a word, such as spaces, symbols, and punctuation, have word breaks on both sides. In such cases, this method will return a text range that contains the given character offset.

Word boundaries are defined more precisely in Unicode Standard Annex #29 http://www.unicode.org/reports/tr29/#Word_Boundaries.

fun getCursorRect(offset: Int): Rect

Returns the rectangle of the cursor area

Parameters

offsetAn character offset of the cursor

Returns

a rectangle of cursor region
fun getPathForRange(start: Int, end: Int): Path

Returns path that enclose the given text range.

Parameters

startan inclusive start character offset
endan exclusive end character offset

Returns

a drawing path
fun copy(
        layoutInput: TextLayoutInput = this.layoutInput,
        size: IntSize = this.size,
    ): TextLayoutResult