Created
August 14, 2023 16:36
-
-
Save chiragthummar/d3cd92936be47d67486cd600ed1d745d to your computer and use it in GitHub Desktop.
TextField Styling Text Without Cyclic Recomposition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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