---
title: "maxLengthTrim"
description: "Limits the total length of the text field to [maxLength] characters by truncating inserted characters."
type: "function"
lastmod: "2026-09-24"
---
## API Reference

### maxLengthTrim

> Source set: Common

```kotlin
public fun InputTransformation.maxLengthTrim(maxLength: Int): InputTransformation
```

Limits the total length of the text field to [maxLength](/jetpack-compose/androidx.compose.foundation/foundation/functions/maxLength/api) characters by truncating inserted
characters.

When text is inserted (e.g. by typing or pasting) that would cause the total length to exceed
[maxLength](/jetpack-compose/androidx.compose.foundation/foundation/functions/maxLength/api), only enough inserted characters are kept to fill the text field up to [maxLength](/jetpack-compose/androidx.compose.foundation/foundation/functions/maxLength/api),
and excess characters are trimmed and discarded. For example, pasting "12345" into a text field
that already has 8 characters and a [maxLength](/jetpack-compose/androidx.compose.foundation/foundation/functions/maxLength/api) of 10 will insert only "12", resulting in 10
characters.

This transformation sets the maximum text length for accessibility services.
[OutputTransformation](/jetpack-compose/androidx.compose.foundation/foundation/interfaces/OutputTransformation/api) does not affect this limit. When using an [OutputTransformation](/jetpack-compose/androidx.compose.foundation/foundation/interfaces/OutputTransformation/api) that adds
decorating characters, set `SemanticsPropertyReceiver.maxTextLength` manually in a custom
[InputTransformation](/jetpack-compose/androidx.compose.foundation/foundation/interfaces/InputTransformation/api) to include those characters in the announced limit.

## Code Examples

### BasicTextFieldInputTransformationMaxLengthCustom

```kotlin
@Sampled
@Composable
fun BasicTextFieldInputTransformationMaxLengthCustom() {
    val state = remember { TextFieldState() }
    BasicTextField(
        state,
        inputTransformation =
            object : InputTransformation {
                override fun SemanticsPropertyReceiver.applySemantics() {
                    // The output transformation formats "1234567890" to "(123) 456-7890",
                    // which is 14 characters long. We set the accessibility maximum length
                    // to 14 so screen readers announce the correct limit.
                    maxTextLength = 14
                }

                override fun TextFieldBuffer.transformInput() {
                    if (length > 10) {
                        delete(10, length)
                    }
                }
            },
        outputTransformation =
            OutputTransformation {
                if (length > 0) insert(0, "(")
                if (length > 4) insert(4, ") ")
                if (length > 9) insert(9, "-")
            },
    )
}
```
