TextAutoSize


Interface used by Text composables to override text size to automatically grow or shrink text to fill the layout bounds.

import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.size import androidx.compose.foundation.text.BasicText import androidx.compose.foundation.text.TextAutoSize import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp Box(modifier = Modifier.size(200.dp)) {  // The text will find the biggest available font size that fits in the box.  BasicText(text = "Hello World", autoSize = TextAutoSize.StepBased()) }

Summary

Public companion functions

TextAutoSize
StepBased(minFontSize: TextUnit, maxFontSize: TextUnit, stepSize: TextUnit)

Automatically size the text with the biggest font size that fits the available space.

Cmn

Public functions

operator Boolean
equals(other: Any?)

This type is used in performance-sensitive paths and requires providing equality guarantees.

Cmn
TextUnit
TextAutoSizeLayoutScope.getFontSize(
    constraints: Constraints,
    text: AnnotatedString
)

Calculates font size and provides access to TextAutoSizeLayoutScope, which offers TextAutoSizeLayoutScope.performLayout to lay out the text and use the measured size.

Cmn
Int

This type is used in performance-sensitive paths and requires providing identity guarantees.

Cmn

Public companion functions

StepBased

fun StepBased(
    minFontSize: TextUnit = TextAutoSizeDefaults.MinFontSize,
    maxFontSize: TextUnit = TextAutoSizeDefaults.MaxFontSize,
    stepSize: TextUnit = 0.25.sp
): TextAutoSize

Automatically size the text with the biggest font size that fits the available space.

When text auto size is performed with TextOverflow.Ellipsis, TextOverflow.StartEllipsis or TextOverflow.MiddleEllipsis (e.g. by specifying textOverflow on BasicText), this implementation will consider the text to be fitting the available space if it is not ellipsized.

Parameters
minFontSize: TextUnit = TextAutoSizeDefaults.MinFontSize

The smallest potential font size of the text. Default = 12.sp. This must be smaller than maxFontSize; an IllegalArgumentException will be thrown otherwise.

maxFontSize: TextUnit = TextAutoSizeDefaults.MaxFontSize

The largest potential font size of the text. Default = 112.sp. This must be larger than minFontSize; an IllegalArgumentException will be thrown otherwise.

stepSize: TextUnit = 0.25.sp

The smallest difference between potential font sizes. Specifically, every font size, when subtracted by minFontSize, is divisible by stepSize. Default = 0.25.sp. This must not be less than 0.0001f.sp; an IllegalArgumentException will be thrown otherwise. If stepSize is greater than the difference between minFontSize and maxFontSize, minFontSize will be used for the layout.

Returns
TextAutoSize

AutoSize instance with the step-based configuration. Using this in a compatible composable will cause its text to be sized as above.

Public functions

equals

operator fun equals(other: Any?): Boolean

This type is used in performance-sensitive paths and requires providing equality guarantees. Using a data class is sufficient. Singletons may implement this function with referential equality (this === other). Instances with no properties may implement this function by checking the type of the other object.

Returns
Boolean

true if both AutoSize instances are identical.

fun TextAutoSizeLayoutScope.getFontSize(
    constraints: Constraints,
    text: AnnotatedString
): TextUnit

Calculates font size and provides access to TextAutoSizeLayoutScope, which offers TextAutoSizeLayoutScope.performLayout to lay out the text and use the measured size.

import androidx.compose.foundation.layout.size import androidx.compose.foundation.text.BasicText import androidx.compose.foundation.text.TextAutoSize import androidx.compose.foundation.text.modifiers.TextAutoSizeLayoutScope import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.unit.Constraints import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.sp data class PresetsTextAutoSize(private val presets: Array<TextUnit>) : TextAutoSize {  override fun TextAutoSizeLayoutScope.getFontSize(  constraints: Constraints,  text: AnnotatedString,  ): TextUnit {  var lastTextSize: TextUnit = TextUnit.Unspecified  for (presetSize in presets) {  val layoutResult = performLayout(constraints, text, presetSize)  if (  layoutResult.size.width <= constraints.maxWidth &&  layoutResult.size.height <= constraints.maxHeight  ) {  lastTextSize = presetSize  } else {  break  }  }  return lastTextSize  } } @Composable fun App() {  val autoSize = remember { PresetsTextAutoSize(arrayOf(10.sp, 14.sp, 21.sp)) }  BasicText(text = "Hello World", autoSize = autoSize) }
Returns
TextUnit

The derived optimal font size

See also
performLayout

hashCode

fun hashCode(): Int

This type is used in performance-sensitive paths and requires providing identity guarantees.

Returns
Int

a unique hashcode for this AutoSize instance.