Skip to content

Instantly share code, notes, and snippets.

@cp-radhika-s
Created November 8, 2022 09:10
Show Gist options
  • Save cp-radhika-s/e247e5d86020295dc6106cbf9e25ed07 to your computer and use it in GitHub Desktop.
Save cp-radhika-s/e247e5d86020295dc6106cbf9e25ed07 to your computer and use it in GitHub Desktop.
@OptIn(ExperimentalTextApi::class)
@Composable
fun ExampleTextAnnotatedString() {
val textMeasure = rememberTextMeasurer()
val text = buildAnnotatedString {
withStyle(
style = SpanStyle(
color = Color.White,
fontSize = 22.sp,
fontStyle = FontStyle.Italic
)
) {
append("Hello,")
}
withStyle(
style = SpanStyle(
brush = Brush.horizontalGradient(colors = RainbowColors),
fontSize = 28.sp,
fontWeight = FontWeight.Bold
)
) {
append("\nText on Canvas️")
}
}
Canvas(modifier = Modifier
.fillMaxWidth()
.height(100.dp), onDraw = {
drawRect(color = Color.Black)
drawText(
textMeasurer = textMeasure,
text = text,
topLeft = Offset(10.dp.toPx(), 10.dp.toPx())
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment