padding swiftui in swift

To apply padding using SwiftUI in Swift, you can use the .padding() modifier on any SwiftUI View.

Here's an example:

main.swift
Text("Hello, World!")
    .padding()
37 chars
3 lines

This will add a default padding of 10 points to all sides of the Text view.

You can also customize the amount of padding on each side by passing in the desired padding amount as a parameter:

main.swift
Text("Hello, World!")
    .padding(.top, 20)
    .padding(.horizontal, 30)
    .padding(.bottom, 10)
101 chars
5 lines

In this example, the .padding(.top, 20) adds 20 points of padding to the top of the view, .padding(.horizontal, 30) adds 30 points of padding to the left and right sides of the view, and .padding(.bottom, 10) adds 10 points of padding to the bottom of the view.

You can also use the shorthand method by passing in the padding amounts in the order top, leading, bottom, trailing:

main.swift
Text("Hello, World!")
    .padding(20, 30, 10, 30)
51 chars
3 lines

In this example, 20 is the padding amount on the top, 30 is the padding amount on the left side, 10 is the padding amount on the bottom, and 30 is the padding amount on the right side.

related categories

gistlibby LogSnag