TextMeasurer


Measures text to prepare it for drawing.

Create instances using rememberTextMeasurer in Composable contexts to inherit default composition locals.

Caches layout results internally using an LRU cache to optimize repeated measure calls.

Reuses the cached layout when changing only draw-affected parameters:

This makes animating draw-only parameters highly efficient.

Layout-affecting changes like text, font size, or constraints calculate a new layout. These new layouts may evict older entries once the cache reaches capacity.

Provide FontFamily.Resolver, LayoutDirection, and Density during construction. TextMeasurer uses these as defaults when skipped in measure calls.

Summary

Public constructors

TextMeasurer(
    defaultFontFamilyResolver: FontFamily.Resolver,
    defaultDensity: Density,
    defaultLayoutDirection: LayoutDirection,
    cacheSize: Int
)
Cmn

Public functions

TextLayoutResult
measure(
    text: String,
    style: TextStyle,
    overflow: TextOverflow,
    softWrap: Boolean,
    maxLines: Int,
    constraints: Constraints,
    layoutDirection: LayoutDirection,
    density: Density,
    fontFamilyResolver: FontFamily.Resolver,
    skipCache: Boolean
)

Creates a TextLayoutResult according to given parameters.

Cmn
TextLayoutResult
measure(
    text: AnnotatedString,
    style: TextStyle,
    overflow: TextOverflow,
    softWrap: Boolean,
    maxLines: Int,
    placeholders: List<AnnotatedString.Range<Placeholder>>,
    constraints: Constraints,
    layoutDirection: LayoutDirection,
    density: Density,
    fontFamilyResolver: FontFamily.Resolver,
    skipCache: Boolean
)

Creates a TextLayoutResult according to given parameters.

Cmn

Public constructors

TextMeasurer

TextMeasurer(
    defaultFontFamilyResolver: FontFamily.Resolver,
    defaultDensity: Density,
    defaultLayoutDirection: LayoutDirection,
    cacheSize: Int = DefaultCacheSize
)
Parameters
defaultFontFamilyResolver: FontFamily.Resolver

resolver to load fonts defined in styles

defaultDensity: Density

density of the measurement environment, used for scaling fonts

defaultLayoutDirection: LayoutDirection

layout direction of the measurement environment

cacheSize: Int = DefaultCacheSize

sets the maximum number of cached layouts. Match this to the number of distinct layouts calculated repeatedly

Public functions

measure

fun measure(
    text: String,
    style: TextStyle = TextStyle.Default,
    overflow: TextOverflow = TextOverflow.Clip,
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
    constraints: Constraints = Constraints(),
    layoutDirection: LayoutDirection = this.defaultLayoutDirection,
    density: Density = this.defaultDensity,
    fontFamilyResolver: FontFamily.Resolver = this.defaultFontFamilyResolver,
    skipCache: Boolean = false
): TextLayoutResult

Creates a TextLayoutResult according to given parameters.

This function supports laying out text that consists of multiple paragraphs, includes placeholders, wraps around soft line breaks, and might overflow outside the specified size.

Most parameters for text affect the final text layout. One pixel change in constraints boundaries can displace a word to another line which would cause a chain reaction that completely changes how text is rendered.

On the other hand, some attributes only play a role when drawing the created text layout. For example text layout can be created completely in black color but we can apply TextStyle.color later in draw phase. This also means that animating text color shouldn't invalidate text layout.

Thus, textLayoutCache helps in the process of converting a set of text layout inputs to a text layout while ignoring non-layout-affecting attributes. Iterative calls that use the same input parameters should benefit from substantial performance improvements.

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.sp

textMeasurer.measure(
    text = "Hello, World",
    style = TextStyle(color = Color.Red, fontSize = 16.sp, fontFamily = FontFamily.Cursive),
    constraints = Constraints(minWidth = 400, maxWidth = 400, minHeight = 200, maxHeight = 400),
)
Parameters
text: String

the text to be laid out

style: TextStyle = TextStyle.Default

the TextStyle to be applied to the whole text

overflow: TextOverflow = TextOverflow.Clip

How visual overflow should be handled.

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.

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 will be truncated according to overflow and softWrap. If it is not null, then it must be greater than zero.

constraints: Constraints = Constraints()

how wide and tall the text is allowed to be. Constraints.maxWidth will define the width of the MultiParagraph. Constraints.maxHeight helps defining the number of lines that fit with ellipsis is true. Constraints.minWidth defines the minimum width the resulting TextLayoutResult.size will report. Constraints.minHeight is no-op.

layoutDirection: LayoutDirection = this.defaultLayoutDirection

layout direction of the measurement environment. If not specified, defaults to the value that was given during initialization of this TextMeasurer.

density: Density = this.defaultDensity

density of the measurement environment. If not specified, defaults to the value that was given during initialization of this TextMeasurer.

fontFamilyResolver: FontFamily.Resolver = this.defaultFontFamilyResolver

to be used to load the font given in SpanStyles. If not specified, defaults to the value that was given during initialization of this TextMeasurer.

skipCache: Boolean = false

Disables cache optimization if it is passed as true.

measure

fun measure(
    text: AnnotatedString,
    style: TextStyle = TextStyle.Default,
    overflow: TextOverflow = TextOverflow.Clip,
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
    placeholders: List<AnnotatedString.Range<Placeholder>> = listOf(),
    constraints: Constraints = Constraints(),
    layoutDirection: LayoutDirection = this.defaultLayoutDirection,
    density: Density = this.defaultDensity,
    fontFamilyResolver: FontFamily.Resolver = this.defaultFontFamilyResolver,
    skipCache: Boolean = false
): TextLayoutResult

Creates a TextLayoutResult according to given parameters.

This function supports laying out text that consists of multiple paragraphs, includes placeholders, wraps around soft line breaks, and might overflow outside the specified size.

Most parameters for text affect the final text layout. One pixel change in constraints boundaries can displace a word to another line which would cause a chain reaction that completely changes how text is rendered.

On the other hand, some attributes only play a role when drawing the created text layout. For example text layout can be created completely in black color but we can apply TextStyle.color later in draw phase. This also means that animating text color shouldn't invalidate text layout.

Thus, textLayoutCache helps in the process of converting a set of text layout inputs to a text layout while ignoring non-layout-affecting attributes. Iterative calls that use the same input parameters should benefit from substantial performance improvements.

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.sp

textMeasurer.measure(
    text =
        buildAnnotatedString {
            append("Hello, ")
            withStyle(SpanStyle(color = Color.Blue)) { append("World!") }
        },
    style = TextStyle(fontSize = 16.sp, fontFamily = FontFamily.Monospace),
)
Parameters
text: AnnotatedString

the text to be laid out

style: TextStyle = TextStyle.Default

the TextStyle to be applied to the whole text

overflow: TextOverflow = TextOverflow.Clip

How visual overflow should be handled.

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.

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 will be truncated according to overflow and softWrap. If it is not null, then it must be greater than zero.

placeholders: List<AnnotatedString.Range<Placeholder>> = listOf()

a list of Placeholders that specify ranges of text which will be skipped during layout and replaced with Placeholder. It's required that the range of each Placeholder doesn't cross paragraph boundary, otherwise IllegalArgumentException is thrown.

constraints: Constraints = Constraints()

how wide and tall the text is allowed to be. Constraints.maxWidth will define the width of the MultiParagraph. Constraints.maxHeight helps defining the number of lines that fit with ellipsis is true. Constraints.minWidth defines the minimum width the resulting TextLayoutResult.size will report. Constraints.minHeight is no-op.

layoutDirection: LayoutDirection = this.defaultLayoutDirection

layout direction of the measurement environment. If not specified, defaults to the value that was given during initialization of this TextMeasurer.

density: Density = this.defaultDensity

density of the measurement environment. If not specified, defaults to the value that was given during initialization of this TextMeasurer.

fontFamilyResolver: FontFamily.Resolver = this.defaultFontFamilyResolver

to be used to load the font given in SpanStyles. If not specified, defaults to the value that was given during initialization of this TextMeasurer.

skipCache: Boolean = false

Disables cache optimization if it is passed as true.