Swift ZStackを利用してテキストの背景に円を表示するサンプル

環境
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
ZStack {
Circle()
Text("文字列")
}
ZStack { Circle() Text("文字列") }
ZStack {
    Circle()
    Text("文字列")
}

TextをZStack内に入れます。Textの前にCircle()を配置します。
ZStackを使って、テキストの背景に円を表示します。

使用例

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

 

Swift

Posted by arkgame