Swift clipShape修飾子を使って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(color.clipShape(Circle()))
Text("文字列") .background(color.clipShape(Circle()))
Text("文字列")
    .background(color.clipShape(Circle()))

Textにbackground修飾子を付与し、background修飾子の引数に背景色を指定します。
背景色にclipShape修飾子を付与します。

使用例

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 Skill")
.foregroundColor(.white)
.frame(width: 110, height: 110)
.background(Color.red.clipShape(Circle()))
}
}
}
struct ContentView: View { var body: some View { VStack { Text("Study, Swift Skill") .foregroundColor(.white) .frame(width: 110, height: 110) .background(Color.red.clipShape(Circle())) } } }
struct ContentView: View {
    var body: some View {
        VStack {
            Text("Study, Swift Skill")
                .foregroundColor(.white)
                .frame(width: 110, height: 110)
                .background(Color.red.clipShape(Circle()))
                
        }
    }
}

 

Swift

Posted by arkgame