JetpackComposeで途中の文字に対して違う色を設定する方法です。
リファレンス
コード
リファレンスの公式サンプルコードを、もう少しカラフルにしてみました。
.kt
@Composable
fun MultipleStylesInText() {
Text(
buildAnnotatedString {
withStyle(style = SpanStyle(color = Color.Green)) {
append("H")
}
withStyle(style = SpanStyle(color = Color.Cyan)) {
append("ello")
}
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold, color = Color.Red)) {
append("W")
}
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold, color = Color.Magenta)) {
append("orld")
}
}
)
}
こんな感じになりました。
あまり使う場面は少ないかもしれませんが、いろいろできそうですね。