[SwiftUI]Text(テキスト)の色を変えるには?

SwiftUI

どうも、ちょげ(@chogetarou)です。

Textビューの色を変える方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

文字色

Textの文字色を変えるには、foregroundColor修飾子を使います。

まず、TextにforegroundColor修飾子を付与します。

そして、foregroundColor修飾子の引数に色を指定します。

Text("テキスト")
     .foregroundColor(色)

使用例

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, SwiftUI")
                .foregroundColor(.red)
        }
    }
}

背景色

Textの背景色を変えるには、background修飾子を使います。

まず、Textにbackground修飾子を付与します。

そして、background修飾子の引数に色を指定します。

Text("テキスト")
    .background(色)

使用例

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, SwiftUI")
                .padding()
                .foregroundColor(.white)
                .background(Color.blue)
        }
    }
}

まとめ

Textの文字色はforegroundColor修飾子、背景色はbackgroud修飾子で変えます。

コメント

タイトルとURLをコピーしました