Join us for ⁠#Android11: The Beta Launch Show on June 3!

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, maxLines: Int = Int.MAX_VALUE, softWrap: Boolean = true, overflow: TextOverflow = TextOverflow.Clip, density: Density, layoutDirection: LayoutDirection, resourceLoader: Font.ResourceLoader)

An object that paints text onto a Canvas.

Public methods

TextLayoutResult
layout(constraints: Constraints, prevResult: TextLayoutResult? = null)

Unit

Unit
paint(canvas: Canvas, textLayoutResult: TextLayoutResult)

Paints the text onto the given canvas.

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

Draws text background of the given range.

Properties

Density

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.

Font.ResourceLoader

Boolean

Whether the text should break at soft line breaks.

TextStyle

AnnotatedString

the text to paint.

Public constructors

<init>

TextDelegate(
    text: AnnotatedString,
    style: TextStyle,
    maxLines: Int = Int.MAX_VALUE,
    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 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.
maxLines: Int = Int.MAX_VALUE 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

layout

fun layout(
    constraints: Constraints,
    prevResult: TextLayoutResult? = null
): TextLayoutResult

layoutIntrinsics

fun layoutIntrinsics(): Unit

paint

fun paint(
    canvas: Canvas,
    textLayoutResult: TextLayoutResult
): 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 SpanStyle when creating the AnnotatedString that you pass to the TextDelegate constructor or to the text property.

paintBackground

fun paintBackground(
    start: Int,
    end: Int,
    color: Color,
    canvas: Canvas,
    textLayoutResult: TextLayoutResult
): 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.

Properties

density

val density: Density

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.

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.

style

val style: TextStyle

text

val text: AnnotatedString

the text to paint.