1
1

JetpackCompose Textの1文字だけ色変える方法

Last updated at Posted at 2023-09-24

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")
            }
        }
    )
}

スクリーンショット 2023-09-24 17.26.51.png

こんな感じになりました。
あまり使う場面は少ないかもしれませんが、いろいろできそうですね。

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1