<!--
{
  "availability" : [
    "macOS: 12.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextAttachmentLayout/image(for:attributes:location:textContainer:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(pl)NSTextAttachmentLayout(im)imageForBounds:attributes:location:textContainer:"
  },
  "title" : "image(for:attributes:location:textContainer:)"
}
-->

# image(for:attributes:location:textContainer:)

Returns the image object rendered at the bounds and inside the text container you specify.

```
func image(for bounds: CGRect, attributes: [NSAttributedString.Key : Any] = [:], location: any NSTextLocation, textContainer: NSTextContainer?) -> NSImage?
```

## Parameters

`bounds`

The <doc://com.apple.documentation/documentation/CoreFoundation/CGRect> that presents the image boundaries inside `textContainer`.

`attributes`

A dictionary of <doc://com.apple.documentation/documentation/Foundation/NSAttributedString/Key> attributes.

`location`

An [`NSTextLocation`](/documentation/AppKit/NSTextLocation) that indicates that start of the string.

`textContainer`

The [`NSTextContainer`](/documentation/AppKit/NSTextContainer) that contains the source text.

## Return Value

An optional image object.

## Discussion

A custom implementation should return an image appropriate for the target rendering context that you derive by arguments to this method. The default [`NSTextAttachment`](/documentation/AppKit/NSTextAttachment) implementation returns the contents of the `image` property when non-`nil`. If the `image` property is `nil`, it returns an image based on the `contents` and `fileType` properties.

---

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)