TextDelegate

class TextDelegate
kotlin.Any
   ↳ androidx.ui.text.TextDelegate

An object that paints text onto a Canvas.

To use a TextDelegate, follow these steps:

  1. Create an AnnotatedString and pass it to the TextDelegate constructor.
  2. Call layout to prepare the paragraph.
  3. Call paint as often as desired to paint the paragraph.

If the width of the area into which the text is being painted changes, return to step 2. If the text to be painted changes, return to step 1.

Summary

Public constructors

<init>(text: AnnotatedString, style: TextStyle? = null, paragraphStyle: ParagraphStyle? = null, maxLines: Int? = null, softWrap: Boolean = true, overflow: TextOverflow = TextOverflow.Clip, density: Density, layoutDirection: LayoutDirection, resourceLoader: Font.ResourceLoader)

An object that paints text onto a Canvas.

Public methods

TextDirection

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

Rect

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

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

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

Int

Returns the character offset closest to the given graphical position.

TextDirection

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

TextRange

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

Unit
layout(constraints: Constraints)

Unit

Unit
paint(canvas: Canvas)

Paints the text onto the given canvas.

Unit
paintBackground(start: Int, end: Int, color: Color, canvas: Canvas)

Draws text background of the given range.

Unit
paintCursor(offset: Int, canvas: Canvas)

Draws the cursor at the given character offset.

Properties

Density

IntPx

The vertical space from the top of text box to the baseline of the first line.

IntPx

The vertical space required to paint this text.

IntPx

The vertical space from the top of text box to the baseline of the last line.

LayoutDirection

The composable layout direction.

IntPx

The width at which increasing the width of the text no longer decreases the height.

Int?

An optional maximum number of lines for the text to span, wrapping if necessary.

IntPx

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

TextOverflow

How visual overflow should be handled.

ParagraphStyle

The paragraph style.

Font.ResourceLoader

Boolean

Whether the text should break at soft line breaks.

AnnotatedString

the text to paint.

TextStyle

The resolved text style.

IntPx

The horizontal space required to paint this text.

Public constructors

<init>

TextDelegate(
    text: AnnotatedString,
    style: TextStyle? = null,
    paragraphStyle: ParagraphStyle? = null,
    maxLines: Int? = null,
    softWrap: Boolean = true,
    overflow: TextOverflow = TextOverflow.Clip,
    density: Density,
    layoutDirection: LayoutDirection,
    resourceLoader: Font.ResourceLoader)

An object that paints text onto a Canvas.

To use a TextDelegate, follow these steps:

  1. Create an AnnotatedString and pass it to the TextDelegate constructor.
  2. Call layout to prepare the paragraph.
  3. Call paint as often as desired to paint the paragraph.

If the width of the area into which the text is being painted changes, return to step 2. If the text to be painted changes, return to step 1.

Parameters
text: AnnotatedString the text to paint.
style: TextStyle? = null The text style specified to render the text. Notice that you can also set text style on the given AnnotatedString, and the style set on text always has higher priority than this setting. But if only one global text style is needed, passing it to TextDelegate is always preferred.
paragraphStyle: ParagraphStyle? = null style configuration that applies only to paragraphs such as text alignment, or text direction.
maxLines: Int? = null An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it is truncated such that subsequent lines are dropped.
softWrap: Boolean = true Whether the text should break at soft line breaks. If false, the glyphs in the text will be positioned as if there was unlimited horizontal space. If softWrap is false, overflow and TextAlign may have unexpected effects.
overflow: TextOverflow = TextOverflow.Clip How visual overflow should be handled. Specifically, the ellipsis is applied to the last line before the line truncated by maxLines, if maxLines is non-null and that line overflows the width constraint.
layoutDirection: LayoutDirection The composable layout direction.

Public methods

getBidiRunDirection

fun getBidiRunDirection(offset: Int): TextDirection

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

getBoundingBox

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.

Valid only after layout has been called.

getHorizontalPosition

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

Get the horizontal position for the specified text offset.

getLineBottom

fun getLineBottom(lineIndex: Int): Float

Returns the bottom y coordinate of the given 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.

getOffsetForPosition

fun getOffsetForPosition(position: PxPosition): Int

Returns the character offset closest to the given graphical position.

getParagraphDirection

fun getParagraphDirection(offset: Int): TextDirection

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

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

layout

fun layout(constraints: Constraints): Unit

layoutIntrinsics

fun layoutIntrinsics(): Unit

paint

fun paint(canvas: Canvas): Unit

Paints the text onto the given canvas.

Valid only after layout has been called.

If you cannot see the text being painted, check that your text color does not conflict with the background on which you are drawing. The default text color is white (to contrast with the default black background color), so if you are writing an application with a white background, the text will not be visible by default.

To set the text style, specify a TextStyle when creating the TextSpan that you pass to the TextDelegate constructor or to the text property.

paintBackground

fun paintBackground(
    start: Int,
    end: Int,
    color: Color,
    canvas: Canvas
): Unit

Draws text background of the given range.

If the given range is empty, do nothing.

Parameters
start: Int inclusive start character offset of the drawing range.
end: Int exclusive end character offset of the drawing range.
color: Color a color to be used for drawing background.
canvas: Canvas the target canvas.

paintCursor

fun paintCursor(
    offset: Int,
    canvas: Canvas
): Unit

Draws the cursor at the given character offset.

Parameters
offset: Int the cursor offset in the text.
canvas: Canvas the target canvas.

Properties

density

val density: Density

firstBaseline

val firstBaseline: IntPx

The vertical space from the top of text box to the baseline of the first line.

Valid only after layout has been called.

height

val height: IntPx

The vertical space required to paint this text.

Valid only after layout has been called.

lastBaseline

val lastBaseline: IntPx

The vertical space from the top of text box to the baseline of the last line.

Valid only after layout has been called.

layoutDirection

val layoutDirection: LayoutDirection

The composable layout direction.

maxIntrinsicWidth

val maxIntrinsicWidth: IntPx

The width at which increasing the width of the text no longer decreases the height.

Valid only after layout has been called.

maxLines

val maxLines: Int?

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it is truncated such that subsequent lines are dropped.

minIntrinsicWidth

val minIntrinsicWidth: IntPx

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

Valid only after layout has been called.

overflow

val overflow: TextOverflow

How visual overflow should be handled. Specifically, the ellipsis is applied to the last line before the line truncated by maxLines, if maxLines is non-null and that line overflows the width constraint.

paragraphStyle

val paragraphStyle: ParagraphStyle

The paragraph style.

If null is passed to constructor, use default paragraph style.

resourceLoader

val resourceLoader: Font.ResourceLoader

softWrap

val softWrap: Boolean

Whether the text should break at soft line breaks. If false, the glyphs in the text will be positioned as if there was unlimited horizontal space. If softWrap is false, overflow and TextAlign may have unexpected effects.

text

val text: AnnotatedString

the text to paint.

textStyle

val textStyle: TextStyle

The resolved text style.

width

val width: IntPx

The horizontal space required to paint this text.

Valid only after layout has been called.