「SwiftUI」EdgeInsetsで各辺の余白サイズを個別に指定する

書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
.padding(EdgeInsets(
top: サイズ, // 上の余白
leading: サイズ, // 左の余白
bottom: サイズ, // 下の余白
trailing: サイズ // 右の余白
))
.padding(EdgeInsets( top: サイズ, // 上の余白 leading: サイズ, // 左の余白 bottom: サイズ, // 下の余白 trailing: サイズ // 右の余白 ))
.padding(EdgeInsets(
  top: サイズ,        // 上の余白
  leading: サイズ,    // 左の余白
  bottom: サイズ,     // 下の余白
  trailing: サイズ    // 右の余白
  ))

EdgeInsetsを使って指定して、各辺の余白サイズを指定します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
struct ContentView: View {
var body: some View {
Text("検証")
.font(.largeTitle)
.padding(EdgeInsets(
top: 10, // 上の余白
leading: 50, // 左の余白
bottom: 10, // 下の余白
trailing: 50 // 右の余白
))
.background(Color.skyblue)
}
}
struct ContentView: View { var body: some View { Text("検証") .font(.largeTitle) .padding(EdgeInsets( top: 10, // 上の余白 leading: 50, // 左の余白 bottom: 10, // 下の余白 trailing: 50 // 右の余白 )) .background(Color.skyblue) } }
struct ContentView: View {
    var body: some View {
        Text("検証")
            .font(.largeTitle)
            .padding(EdgeInsets(
                top: 10,         // 上の余白
                leading: 50,    // 左の余白
                bottom: 10,      // 下の余白
                trailing: 50    // 右の余白
            ))
            .background(Color.skyblue)
    }
}

実行結果
文字「検証」の条件左右に余白のサイズを個別に指定します。

Swift

Posted by arkgame