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

Paragraph

interface Paragraph
androidx.compose.ui.text.Paragraph

A paragraph of text that is laid out.

Paragraphs can be displayed on a Canvas using the paint method.

Summary

Public methods
abstract ResolvedTextDirection

Get the text direction of the character at the given offset.

abstract Rect

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

abstract Rect
getCursorRect(offset: Int)

Returns rectangle of the cursor area.

abstract Float
getHorizontalPosition(offset: Int, usePrimaryDirection: Boolean)

Compute the horizontal position where a newly inserted character at offset would be.

abstract Float
getLineBottom(lineIndex: Int)

Returns the bottom y coordinate of the given line.

abstract Int
getLineEnd(lineIndex: Int)

Returns the end offset of the given line

abstract Int

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

abstract Int

Returns line number closest to the given graphical vertical position.

abstract Float
getLineHeight(lineIndex: Int)

Returns the height of the given line.

abstract Float
getLineLeft(lineIndex: Int)

Returns the left x Coordinate of the given line.

abstract Float
getLineRight(lineIndex: Int)

Returns the right x Coordinate of the given line.

abstract Int
getLineStart(lineIndex: Int)

Returns the start offset of the given line, inclusive.

abstract Float
getLineTop(lineIndex: Int)

Returns the bottom y coordinate of the given line.

abstract Int
getLineVisibleEnd(lineIndex: Int)

Returns the end of visible offset of the given line.

abstract Float
getLineWidth(lineIndex: Int)

Returns the width of the given line.

abstract Int

Returns the character offset closest to the given graphical position.

abstract ResolvedTextDirection

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

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

Returns path that enclose the given text range.

abstract TextRange

Returns the TextRange of the word at the given character offset.

abstract Boolean
isLineEllipsized(lineIndex: Int)

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

abstract Unit
paint(canvas: Canvas, color: Color = Color.Unspecified, shadow: Shadow? = null, textDecoration: TextDecoration? = null)

Paint the paragraph to canvas, and also overrides some paint settings.

Properties
abstract Boolean

True if there is more vertical content, but the text was truncated, either because we reached maxLines lines of text or because the maxLines was null, ellipsis was not null, and one of the lines exceeded the width constraint.

abstract Float

The distance from the top of the paragraph to the alphabetic baseline of the first line, in logical pixels.

abstract Float

The amount of vertical space this paragraph occupies.

abstract Float

The distance from the top of the paragraph to the alphabetic baseline of the last line, in logical pixels.

abstract Int

The total number of lines in the text.

abstract Float

Returns the smallest width beyond which increasing the width never decreases the height.

abstract Float

The width for text if all soft wrap opportunities were taken.

abstract List<Rect?>

The bounding boxes reserved for the input placeholders in this Paragraphs.

abstract Float

The amount of horizontal space this paragraph occupies.

Public methods

getBidiRunDirection

abstract fun getBidiRunDirection(offset: Int): ResolvedTextDirection

Get the text direction of the character at the given offset.

getBoundingBox

abstract fun getBoundingBox(offset: Int): Rect

Returns the bounding box as Rect of the character for given character offset. Rect includes the top, bottom, left and right of a character.

getCursorRect

abstract fun getCursorRect(offset: Int): Rect

Returns rectangle of the cursor area.

getHorizontalPosition

abstract fun getHorizontalPosition(
    offset: Int,
    usePrimaryDirection: Boolean
): Float

Compute the horizontal position where a newly inserted character at offset would be.

If the inserted character at offset is within a LTR/RTL run, the returned position will be the left(right) edge of the character.

For example:
    Paragraph's direction is LTR.
    Text in logic order:               L0 L1 L2 R3 R4 R5
    Text in visual order:              L0 L1 L2 R5 R4 R3
        position of the offset(2):          |
        position of the offset(4):                   |

However, when the offset is at the BiDi transition offset, there will be two possible visual positions, which depends on the direction of the inserted character.

For example:
    Paragraph's direction is LTR.
    Text in logic order:               L0 L1 L2 R3 R4 R5
    Text in visual order:              L0 L1 L2 R5 R4 R3
        position of the offset(3):             |           (The inserted character is LTR)
                                                        |  (The inserted character is RTL)

In this case, usePrimaryDirection will be used to resolve the ambiguity. If true, the inserted character's direction is assumed to be the same as Paragraph's direction. Otherwise, the inserted character's direction is assumed to be the opposite of the Paragraph's direction.

For example:
    Paragraph's direction is LTR.
    Text in logic order:               L0 L1 L2 R3 R4 R5
    Text in visual order:              L0 L1 L2 R5 R4 R3
        position of the offset(3):             |           (usePrimaryDirection is true)
                                                        |  (usePrimaryDirection is false)

This method is useful to compute cursor position.

Parameters
offset: Int the offset of the character, in the range of 0,length.
usePrimaryDirection: Boolean whether the paragraph direction is respected when offset points to a BiDi transition point.
Return
a float number representing the horizontal position in the unit of pixel.

getLineBottom

abstract fun getLineBottom(lineIndex: Int): Float

Returns the bottom y coordinate of the given line.

getLineEnd

abstract 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

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

getLineForVerticalPosition

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

getLineHeight

abstract fun getLineHeight(lineIndex: Int): Float

Returns the height of the given line.

getLineLeft

abstract fun getLineLeft(lineIndex: Int): Float

Returns the left x Coordinate of the given line.

getLineRight

abstract fun getLineRight(lineIndex: Int): Float

Returns the right x Coordinate of the given line.

getLineStart

abstract fun getLineStart(lineIndex: Int): Int

Returns the start offset of the given line, inclusive.

getLineTop

abstract fun getLineTop(lineIndex: Int): Float

Returns the bottom y coordinate of the given line.

getLineVisibleEnd

abstract 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

getLineWidth

abstract fun getLineWidth(lineIndex: Int): Float

Returns the width of the given line.

getOffsetForPosition

abstract fun getOffsetForPosition(position: Offset): Int

Returns the character offset closest to the given graphical position.

getParagraphDirection

abstract fun getParagraphDirection(offset: Int): ResolvedTextDirection

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

getPathForRange

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

Returns path that enclose the given text range.

getWordBoundary

abstract fun getWordBoundary(offset: Int): TextRange

Returns the TextRange 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 TextRange(offset, offset+1). Word boundaries are defined more precisely in Unicode Standard Annex #29 http://www.unicode.org/reports/tr29/#Word_Boundaries

isLineEllipsized

abstract 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

paint

abstract fun paint(
    canvas: Canvas,
    color: Color = Color.Unspecified,
    shadow: Shadow? = null,
    textDecoration: TextDecoration? = null
): Unit

Paint the paragraph to canvas, and also overrides some paint settings.

Properties

didExceedMaxLines

abstract val didExceedMaxLines: Boolean

True if there is more vertical content, but the text was truncated, either because we reached maxLines lines of text or because the maxLines was null, ellipsis was not null, and one of the lines exceeded the width constraint.

See the discussion of the maxLines and ellipsis arguments at ParagraphStyle.

firstBaseline

abstract val firstBaseline: Float

The distance from the top of the paragraph to the alphabetic baseline of the first line, in logical pixels.

height

abstract val height: Float

The amount of vertical space this paragraph occupies.

lastBaseline

abstract val lastBaseline: Float

The distance from the top of the paragraph to the alphabetic baseline of the last line, in logical pixels.

lineCount

abstract val lineCount: Int

The total number of lines in the text.

maxIntrinsicWidth

abstract val maxIntrinsicWidth: Float

Returns the smallest width beyond which increasing the width never decreases the height.

minIntrinsicWidth

abstract val minIntrinsicWidth: Float

The width for text if all soft wrap opportunities were taken.

placeholderRects

abstract val placeholderRects: List<Rect?>

The bounding boxes reserved for the input placeholders in this Paragraphs. Their locations are relative to this Paragraph's coordinate. The order of this list corresponds to that of input placeholders. Notice that Rect in placeholderRects is nullable. When Rect is null, it indicates that the corresponding Placeholder is ellipsized.

width

abstract val width: Float

The amount of horizontal space this paragraph occupies.