跳转至
Protocol

InsettableShape

A shape type that is able to inset itself to produce another shape.

Declaration

protocol InsettableShape : Shape

Overview

This is the protocol that allows you to inset and stroke shapes.

Types conforming to this protocol include:

Any type conforming to this protocol automatically gets implementations of the strokeBorder(_:style:antialiased:) modifier. This allows you to add a border to a shape without increasing its total size at all.

To conform to this protocol, there is only one requirement: the inset(by:) function. Implement this function to describe how to inset a custom shape.

The following example shows how to conform a custom shape to this protocol, and then use it to stroke the border. (See Shape and path(in:) for more on creating cutom shapes.)

struct ArcView: View {
    var body: some View {
        Arc(endAngle: Angle(degrees: 270))
            .strokeBorder(Color.green)
    }
}

struct Arc: InsettableShape {
    var endAngle: Angle
    var insetAmount: CGFloat = 0

    func path(in rect: CGRect) -> Path {
        Path { p in
            p.addArc(center: CGPoint(x: rect.midX, y: rect.midY),
                     radius: rect.width / 2 - insetAmount,
                     startAngle: .zero,
                     endAngle: endAngle,
                     clockwise: false)
        }
    }

    func inset(by amount: CGFloat) -> some InsettableShape {
        var arc = self
        arc.insetAmount += amount
        return arc
    }
}
A screenshot displaying a thin green arc covering 270 degrees counterclockwise from the top of the screen.

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Instance Method

inset(by:) Returns self inset by amount.

strokeBorder(_:lineWidth:antialiased:) Returns a view that is the result of filling the width-sized border (aka inner stroke) of self with content. This is equivalent to insetting self by width / 2 and stroking the resulting shape with width as the line-width.

strokeBorder(_:style:antialiased:) Returns a view that is the result of insetting self by style.lineWidth / 2, stroking the resulting shape with style, and then filling with content.

strokeBorder(lineWidth:antialiased:) Returns a view that is the result of filling the width-sized border (aka inner stroke) of self with the foreground color. This is equivalent to insetting self by width / 2 and stroking the resulting shape with width as the line-width.

strokeBorder(style:antialiased:) Returns a view that is the result of insetting self by style.lineWidth / 2, stroking the resulting shape with style, and then filling with the foreground color.


Associated Type

InsetShape The type of the inset shape.