Google is committed to advancing racial equity for Black communities. See how.

TextLayoutResult

data class TextLayoutResult
kotlin.Any
   ↳ androidx.compose.ui.text.TextLayoutResult

The data class which holds text layout result.

Summary

Public methods
ResolvedTextDirection

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

Rect

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

Rect
getCursorRect(offset: Int)

Returns the rectangle of the cursor area

Float
getHorizontalPosition(offset: Int, usePrimaryDirection: Boolean)

Get the horizontal position for the specified text offset.

Float
getLineBottom(lineIndex: Int)

Returns the bottom y coordinate of the given line.

Int
getLineEnd(lineIndex: Int)

Returns the end offset of the given line

Int

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

Int

Returns line number closest to the given graphical vertical position.

Float
getLineLeft(lineIndex: Int)

Returns the left x coordinate of the given line.

Float
getLineRight(lineIndex: Int)

Returns the right x coordinate of the given line.

Int
getLineStart(lineIndex: Int)

Returns the end offset of the given line, inclusive.

Float
getLineTop(lineIndex: Int)

Returns the top y coordinate of the given line.

Int
getLineVisibleEnd(lineIndex: Int)

Returns the end of visible offset of the given line.

Int

Returns the character offset closest to the given graphical position.

ResolvedTextDirection

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

Path
getPathForRange(start: Int, end: Int)

Returns path that enclose the given text range.

TextRange

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

Boolean
isLineEllipsized(lineIndex: Int)

Returns true if ellipsis happens on the given line, otherwise returns false

Properties
Boolean

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

Boolean

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

Float

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

Boolean

Returns true if either vertical overflow or horizontal overflow happens.

Float

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

TextLayoutInput

The parameters used for computing this text layout result.

Int

Returns a number of lines of this text layout

List<Rect?>

Returns a list of bounding boxes that is reserved for TextLayoutInput.placeholders.

IntSize

The amount of space required to paint this text in Int.

Public methods

getBidiRunDirection

fun getBidiRunDirection(offset: Int): ResolvedTextDirection

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

Parameters
offset: Int a character offset
Return
the direction of the BiDi run of the given character offset.

getBoundingBox

fun getBoundingBox(offset: Int): Rect

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

Parameters
offset: Int a character offset
Return
a bounding box for the character in pixels.

getCursorRect

fun getCursorRect(offset: Int): Rect

Returns the rectangle of the cursor area

Parameters
offset: Int An character offset of the cursor
Return
a rectangle of cursor region

getHorizontalPosition

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
offset: Int a character offset
usePrimaryDirection: Boolean true for using the primary run's coordinate if the given offset is in the BiDi directional transition point.
Return
the relative distance from the text starting edge.

getLineBottom

fun getLineBottom(lineIndex: Int): Float

Returns the bottom y coordinate of the given line.

Parameters
lineIndex: Int the line number
Return
the line bottom y coordinate

getLineEnd

fun getLineEnd(lineIndex: Int): Int

Returns the end offset of the given line

If ellipsis happens on the given line, this returns the end of text since ellipsized characters are counted into the same line.

Parameters
lineIndex: Int the line number
Return
an exclusive end offset of the line.

getLineForOffset

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
offset: Int a character offset
Return
the 0 origin line number.

getLineForVerticalPosition

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
vertical: Float the vertical position
Return
the 0 origin line number.

getLineLeft

fun getLineLeft(lineIndex: Int): Float

Returns the left x coordinate of the given line.

Parameters
lineIndex: Int the line number
Return
the line left x coordinate

getLineRight

fun getLineRight(lineIndex: Int): Float

Returns the right x coordinate of the given line.

Parameters
lineIndex: Int the line number
Return
the line right x coordinate

getLineStart

fun getLineStart(lineIndex: Int): Int

Returns the end offset of the given line, inclusive.

Parameters
lineIndex: Int the line number
Return
the start offset of the line

getLineTop

fun getLineTop(lineIndex: Int): Float

Returns the top y coordinate of the given line.

Parameters
lineIndex: Int the line number
Return
the line top y coordinate

getLineVisibleEnd

fun getLineVisibleEnd(lineIndex: Int): Int

Returns the end of visible offset of the given line.

If no ellipsis happens on the given line, this returns the line end offset with excluding trailing whitespaces. If ellipsis happens on the given line, this returns the offset that ellipsis started, i.e. the exclusive not ellipsized last character.

Parameters
lineIndex: Int a 0 based line index
Return
an exclusive line end offset that is visible on the display

See Also

getOffsetForPosition

fun getOffsetForPosition(position: Offset): Int

Returns the character offset closest to the given graphical position.

Parameters
position: Offset a graphical position in this text layout
Return
a character offset that is closest to the given graphical position.

getParagraphDirection

fun getParagraphDirection(offset: Int): ResolvedTextDirection

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

Parameters
offset: Int a character offset
Return
the paragraph direction

getPathForRange

fun getPathForRange(
    start: Int,
    end: Int
): Path

Returns path that enclose the given text range.

Parameters
start: Int an inclusive start character offset
end: Int an exclusive end character offset
Return
a drawing path

getWordBoundary

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 <_>.

isLineEllipsized

fun isLineEllipsized(lineIndex: Int): Boolean

Returns true if ellipsis happens on the given line, otherwise returns false

Parameters
lineIndex: Int a 0 based line index
Return
true if ellipsis happens on the given line, otherwise false

Properties

didOverflowHeight

val didOverflowHeight: Boolean

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

didOverflowWidth

val didOverflowWidth: Boolean

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

firstBaseline

val firstBaseline: Float

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

hasVisualOverflow

val hasVisualOverflow: Boolean

Returns true if either vertical overflow or horizontal overflow happens.

lastBaseline

val lastBaseline: Float

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

layoutInput

val layoutInput: TextLayoutInput

The parameters used for computing this text layout result.

lineCount

val lineCount: Int

Returns a number of lines of this text layout

placeholderRects

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.

size

val size: IntSize

The amount of space required to paint this text in Int.