public class TextMeasurer(
private val defaultFontFamilyResolver: FontFamily.Resolver,
private val defaultDensity: Density,
private val defaultLayoutDirection: LayoutDirection,
private val cacheSize: Int = DefaultCacheSize,
)
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:
TextStyle.colororTextStyle.brushTextStyle.shadowTextStyle.textDecorationTextStyle.drawStyle
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.
Parameters
| defaultFontFamilyResolver | resolver to load fonts defined in styles |
| defaultLayoutDirection | layout direction of the measurement environment |
| defaultDensity | density of the measurement environment, used for scaling fonts |
| cacheSize | sets the maximum number of cached layouts. Match this to the number of distinct layouts calculated repeatedly |
Functions
measure
public 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.
Parameters
| text | the text to be laid out |
| style | the TextStyle to be applied to the whole text |
| overflow | How visual overflow should be handled. |
| softWrap | 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 | 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 | 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 | 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 | layout direction of the measurement environment. If not specified, defaults to the value that was given during initialization of this TextMeasurer. |
| density | density of the measurement environment. If not specified, defaults to the value that was given during initialization of this TextMeasurer. |
| fontFamilyResolver | 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 | Disables cache optimization if it is passed as true. |
measure
public 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.
Parameters
| text | the text to be laid out |
| style | the TextStyle to be applied to the whole text |
| overflow | How visual overflow should be handled. |
| softWrap | 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 | 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 | 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 | layout direction of the measurement environment. If not specified, defaults to the value that was given during initialization of this TextMeasurer. |
| density | density of the measurement environment. If not specified, defaults to the value that was given during initialization of this TextMeasurer. |
| fontFamilyResolver | 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 | Disables cache optimization if it is passed as true. |