Skip to content

Instantly share code, notes, and snippets.

@chiragthummar
Created August 14, 2023 16:36
Show Gist options
  • Save chiragthummar/d3cd92936be47d67486cd600ed1d745d to your computer and use it in GitHub Desktop.
Save chiragthummar/d3cd92936be47d67486cd600ed1d745d to your computer and use it in GitHub Desktop.
TextField Styling Text Without Cyclic Recomposition
import androidx.compose.material.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.OffsetMapping
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.input.TransformedText
import androidx.compose.ui.text.withStyle
@Composable
fun StyleEditTextWithoutInfiniteRecomposition() {
val testText = remember { mutableStateOf(TextFieldValue()) }
TextField(
value = testText.value,
onValueChange = {
testText.value = it
},
visualTransformation = {
TransformedText(
text = buildAnnotatedString {
withStyle(SpanStyle(fontWeight = FontWeight.Bold)) {
append(testText.value.text)
}
},
offsetMapping = OffsetMapping.Identity
)
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment