chanjungskim
03/09/2023, 4:42 AMchanjungskim
03/09/2023, 4:42 AM@Composable
fun TypeAnimationText(
text: String,
duration: Long,
modifier: Modifier = Modifier,
style: TextStyle = TextStyle.Default,
color: androidx.compose.ui.graphics.Color = Color.Black,
) {
val transition = updateTransition(targetState = text, label = text)
val caretIndex by transition.animateInt(
transitionSpec = {
tween(durationMillis = duration.toInt()*text.length)
},
label = "caretIndex"
) { targetText ->
Log.d("aos", "targetText: $targetText")
targetText.length
}
// decompose caretIndex to 0
Text(
text = buildAnnotatedString {
withStyle(style = SpanStyle(color = color)) {
append(text.substring(0, caretIndex))
}
withStyle(style = SpanStyle(color = Color.Transparent)) {
append(text.substring(caretIndex, text.length))
}
},
style = style,
modifier = modifier
)
}
Jan
04/12/2023, 9:53 PMchanjungskim
04/13/2023, 5:10 AM