Swift Circle()を使ってTextに円の背景色を設定するサンプル

環境
Swift version 5.2.3 (swift-5.2.3-RELEASE)
Ubuntu 20.04.2 LTS

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Text("文字列")
.background(
Circle()
.foregroundColor(color)
)
Text("文字列") .background( Circle() .foregroundColor(color) )
Text("文字列")
    .background(
        Circle()
             .foregroundColor(color)
        )

background修飾子の引数にCircle()を指定します。
Circle()にforegroundColor修飾子を付与し、foregroundColor修飾子の引数に背景色を指定します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
struct ContentView: View {
var body: some View {
VStack {
Text("Study, Swift")
.foregroundColor(.white)
.padding()
.background(
Circle()
.foregroundColor(.green)
.frame(width: 150, height: 150)
)
}
}
}
struct ContentView: View { var body: some View { VStack { Text("Study, Swift") .foregroundColor(.white) .padding() .background( Circle() .foregroundColor(.green) .frame(width: 150, height: 150) ) } } }
struct ContentView: View {
    var body: some View {
        VStack {
            Text("Study, Swift")
                .foregroundColor(.white)
                .padding()
                .background(
                    Circle()
                        .foregroundColor(.green)
                        .frame(width: 150, height: 150)
                )
        }
    }
}

 

 

Swift

Posted by arkgame