# TextAutoSize

Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/TextAutoSize.kt+class:androidx.compose.foundation.text.TextAutoSize) Added in [1.8.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.8.0) Common/AllAndroid/JVM
- Cmn

  ```
  interface TextAutoSize
  ```

*** ** * ** ***

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

```kotlin
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 |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize#StepBased(androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit)(minFontSize: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/TextUnit, maxFontSize: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/TextUnit, stepSize: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/TextUnit)` Automatically size the text with the biggest font size that fits the available space. | Cmn |

| ### Public functions |
|---|---|---|
| `operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize#equals(kotlin.Any)(other: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?)` This type is used in performance-sensitive paths and requires providing equality guarantees. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/TextUnit` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/modifiers/TextAutoSizeLayoutScope.https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize#(androidx.compose.foundation.text.modifiers.TextAutoSizeLayoutScope).getFontSize(androidx.compose.ui.unit.Constraints,androidx.compose.ui.text.AnnotatedString)( constraints: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints, text: https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString )` Calculates font size and provides access to `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/modifiers/TextAutoSizeLayoutScope`, which offers `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/modifiers/TextAutoSizeLayoutScope#performLayout(androidx.compose.ui.unit.Constraints,androidx.compose.ui.text.AnnotatedString,androidx.compose.ui.unit.TextUnit)` to lay out the text and use the measured size. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize#hashCode()()` This type is used in performance-sensitive paths and requires providing identity guarantees. | Cmn |

## Public companion functions

### StepBased

Cmn Added in [1.10.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.10.0)

```
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 `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/style/TextOverflow#Ellipsis()`, `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/style/TextOverflow#StartEllipsis()` or `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/style/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: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/TextUnit = TextAutoSizeDefaults.MinFontSize` | The smallest potential font size of the text. Default = 12.sp. This must be smaller than `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize.Companion#StepBased(androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit)`; an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` will be thrown otherwise. |
| `maxFontSize: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/TextUnit = TextAutoSizeDefaults.MaxFontSize` | The largest potential font size of the text. Default = 112.sp. This must be larger than `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize.Companion#StepBased(androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit)`; an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` will be thrown otherwise. |
| `stepSize: https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/TextUnit = 0.25.sp` | The smallest difference between potential font sizes. Specifically, every font size, when subtracted by `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize.Companion#StepBased(androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit)`, is divisible by `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize.Companion#StepBased(androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit)`. Default = 0.25.sp. This must not be less than `0.0001f.sp`; an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` will be thrown otherwise. If `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize.Companion#StepBased(androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit)` is greater than the difference between `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize.Companion#StepBased(androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit)` and `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize.Companion#StepBased(androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit)`, `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/TextAutoSize.Companion#StepBased(androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.unit.TextUnit)` will be used for the layout. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/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

Cmn Added in [1.8.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.8.0)

```
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 |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if both AutoSize instances are identical. |

### TextAutoSizeLayoutScope.getFontSize

Cmn Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/TextAutoSize.kt+function:getFontSize) Added in [1.10.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.10.0)

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

Calculates font size and provides access to `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/modifiers/TextAutoSizeLayoutScope`, which offers `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/modifiers/TextAutoSizeLayoutScope#performLayout(androidx.compose.ui.unit.Constraints,androidx.compose.ui.text.AnnotatedString,androidx.compose.ui.unit.TextUnit)` to lay out the text and use the measured size.

```kotlin
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 |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/TextUnit` | The derived optimal font size |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/modifiers/TextAutoSizeLayoutScope#performLayout(androidx.compose.ui.unit.Constraints,androidx.compose.ui.text.AnnotatedString,androidx.compose.ui.unit.TextUnit)` |   |

### hashCode

Cmn Added in [1.8.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.8.0)

```
fun hashCode(): Int
```

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

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | a unique hashcode for this AutoSize instance. |