Created
March 17, 2025 18:40
-
-
Save cavin-macwan/db9928ee9f7d747e7134e2d817829848 to your computer and use it in GitHub Desktop.
Type writer text effect in jetpack compose
This file contains hidden or 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
@Composable | |
fun TypeWriterTextAnimation() { | |
Box( | |
modifier = Modifier | |
.fillMaxSize() | |
.padding(10.dp), | |
contentAlignment = Alignment.Center | |
) { | |
TypewriterText( | |
baseText = "Jetpack Compose is ", | |
parts = listOf("best", "super", "awesome") | |
) | |
} | |
} | |
@Composable | |
fun TypewriterText( | |
baseText: String, | |
parts: List<String> | |
) { | |
var partIndex by remember { mutableIntStateOf(0) } | |
var partText by remember { mutableStateOf("") } | |
val textToDisplay = "$baseText $partText" | |
LaunchedEffect(key1 = parts) { | |
while (partIndex <= parts.size) { | |
val part = parts[partIndex] | |
part.forEachIndexed { charIndex, _ -> | |
partText = part.substring(startIndex = 0, endIndex = charIndex + 1) | |
delay(100) | |
} | |
delay(1000) | |
part.forEachIndexed { charIndex, _ -> | |
partText = part | |
.substring(startIndex = 0, endIndex = part.length - (charIndex + 1)) | |
delay(30) | |
} | |
delay(500) | |
partIndex = (partIndex + 1) % parts.size | |
} | |
} | |
Text( | |
text = textToDisplay, | |
style = TextStyle( | |
fontWeight = FontWeight.SemiBold, | |
fontSize = 40.sp, | |
letterSpacing = -(1.6).sp, | |
lineHeight = 52.sp | |
), | |
color = Color.Black, | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment