<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSCoder/cgRect(for:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:@F@CGRectFromString"
  },
  "title" : "cgRect(for:)"
}
-->

# cgRect(for:)

Returns a Core Graphics rectangle structure corresponding to the data in a given string.

```
class func cgRect(for string: String) -> CGRect
```

## Parameters

`string`

A string whose contents are of the form “{{*x*,*y*},{*w*, *h*}}”, where *x* is the x coordinate, *y* is the y coordinate, *w* is the width, and *h* is the height. These components can represent integer or float values. An example of a valid string is @”{{3,2},{4,5}}”. The string is not localized, so items are always separated with a comma.

## Return Value

A Core Graphics structure that represents a rectangle. If the string is not well-formed, the function returns <doc://com.apple.documentation/documentation/CoreGraphics/CGRectZero>.

## Discussion

In general, you should use this function only to convert strings that were previously created using the [`string(for:)`](/documentation/Foundation/NSCoder/string(for:)-4qz0a) function.

---

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)