Protocol

ShapeStyle

A color or pattern to use when rendering a shape.

Declaration 宣言

protocol ShapeStyle

Overview 概要

You don’t use the ShapeStyle protocol directly. Instead, use one of the concrete styles that SwiftUI defines. To indicate a specific color or pattern, you can use Color or the style returned by image(_:sourceRect:scale:), or one of the gradient types, like the one returned by radialGradient(_:center:startRadius:endRadius:). To set a color that’s appropriate for a given context on a given platform, use one of the semantic styles, like background or primary.

You can use a shape style by:

  • Filling a shape with a style with the fill(_:style:) modifier:

    
    Path { path in
        path.move(to: .zero)
        path.addLine(to: CGPoint(x: 50, y: 0))
        path.addArc(
            center: .zero,
            radius: 50,
            startAngle: .zero,
            endAngle: .degrees(90),
            clockwise: false)
    }
    .fill(.radial(
        Gradient(colors: [.yellow, .red]),
        center: .topLeading,
        startRadius: 15,
        endRadius: 80))

    A screenshot of a quarter of a circle filled with

  • Tracing the outline of a shape with a style with either the stroke(_:lineWidth:) or the stroke(_:style:) modifier:

    
    RoundedRectangle(cornerRadius: 10)
        .stroke(.mint, lineWidth: 10)
        .frame(width: 200, height: 50)

    A screenshot of a rounded rectangle, outlined in mint.

  • Styling the foreground elements in a view with the foregroundStyle(_:) modifier:

    
    VStack(alignment: .leading) {
        Text("Primary")
            .font(.title)
        Text("Secondary")
            .font(.caption)
            .foregroundStyle(.secondary)
    }

    A screenshot of a title in the primary content color above a

Topics 話題

System Colors

Angular Gradients

Elliptical Gradients

Linear Gradients

Radial Gradients

Materials

Image Paint Styles

Hierarchical Styles

Semantic Styles

Mapping to Absolute Coordinates

Using a Shape Style as a View

See Also 参照

Shape Styles