TextSpan در Compose با استفاده از annotatedString
پنجشنبه, ۱۲ بهمن ۱۴۰۲، ۰۲:۳۶ ق.ظ
fun TextCompose_3() {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Black),
contentAlignment = Alignment.Center,
) {
val annotatedString = buildAnnotatedString {
blueGradientText("Hello")
append("\n\n")
pinkBlueGradientText("Maryam")
append("\n\n")
}
Text(text = annotatedString)
}
}
@OptIn(ExperimentalTextApi::class)
private fun AnnotatedString.Builder.blueGradientText(text: String) {
withStyle(
style = SpanStyle(
brush = Brush.linearGradient(
colors = listOf(
Color(0xFF2788C7),
Color(0xFF00BBD4)
)
),
fontSize = 42.sp,
fontWeight = FontWeight.Medium
),
) {
append(text)
}
}
@OptIn(ExperimentalTextApi::class)
private fun AnnotatedString.Builder.pinkBlueGradientText(text: String) {
withStyle(
style = SpanStyle(
brush = Brush.linearGradient(
colors = listOf(
Color(0xFFFF3885),
Color(0xFF00BBD4)
)
),
fontSize = 48.sp,
fontWeight = FontWeight.Bold
)
) {
append(text)
}
}
۰۲/۱۱/۱۲