Generic Instance Method

border(_:width:)

Adds a border to the view with the specified style and width.

Declaration

func border<S>(_ content: S, width: CGFloat = 1) -> some View where S : ShapeStyle

Parameters

content

The border style.

width

The thickness of the border; if not provided, the default is 1 pixel.

Return Value

A view that adds a border with the specified style and width to this view.

Discussion

Use border(_:width:) to draw a border of a specified width around the view’s fame. By default, the border appears inside the bounds of this view. In this example, the four-point border covers the text:

Text("Purple border inside the view bounds.")
    .border(Color.purple, width: 4)
A screenshot showing a border inside the bounds of the text view.

To place a border around the outside of this view, apply padding of the same width before adding the border:

Text("Purple border outside the view bounds.")
    .padding(4)
    .border(Color.purple, width: 4)
A screenshot showing a border outside the text bounds.