<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/ShapeStyle/in(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI10ShapeStylePAAE2inyQrSo6CGRectVF"
  },
  "title" : "in(_:)"
}
-->

# in(_:)

Maps a shape style’s unit-space coordinates to the absolute coordinates
of a given rectangle.

```
func `in`(_ rect: CGRect) -> some ShapeStyle
```

## Parameters

`rect`

A rectangle that gives the absolute coordinates over
which to map the shape style.

## Return Value

A new shape style mapped to the coordinates given by `rect`.

## Discussion

Some shape styles have colors or patterns that vary
with position based on [`UnitPoint`](/documentation/SwiftUI/UnitPoint) coordinates. For example, you
can create a [`LinearGradient`](/documentation/SwiftUI/LinearGradient) using [`top`](/documentation/SwiftUI/UnitPoint/top) and
[`bottom`](/documentation/SwiftUI/UnitPoint/bottom) as the start and end points:

```
let gradient = LinearGradient(
    colors: [.red, .yellow],
    startPoint: .top,
    endPoint: .bottom)
```

When rendering such styles, SwiftUI maps the unit space coordinates to
the absolute coordinates of the filled shape. However, you can tell
SwiftUI to use a different set of coordinates by supplying a rectangle
to the `in(_:)` method. Consider two resizable rectangles using the
gradient defined above:

```
HStack {
    Rectangle()
        .fill(gradient)
    Rectangle()
        .fill(gradient.in(CGRect(x: 0, y: 0, width: 0, height: 300)))
}
.onTapGesture { isBig.toggle() }
.frame(height: isBig ? 300 : 50)
.animation(.easeInOut)
```

When `isBig` is true — defined elsewhere as a private [`State`](/documentation/SwiftUI/State)
variable — the rectangles look the same, because their heights
match that of the modified gradient:

![Two identical, tall rectangles, with a gradient that starts red at](images/com.apple.SwiftUI/ShapeStyle-in-1@2x.png)

When the user toggles `isBig` by tapping the [`HStack`](/documentation/SwiftUI/HStack), the
rectangles shrink, but the gradients each react in a different way:

![Two short rectangles with different coloration. The first has a](images/com.apple.SwiftUI/ShapeStyle-in-2@2x.png)

SwiftUI remaps the gradient of the first rectangle to the new frame
height, so that you continue to see the full range of colors in a
smaller area. For the second rectangle, the modified gradient retains
a mapping to the full height, so you instead see only a small part of
the overall gradient. Animation helps to visualize the difference.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)