<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSAttributedString/attribute(_:at:effectiveRange:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSAttributedString(im)attribute:atIndex:effectiveRange:"
  },
  "title" : "attribute(_:at:effectiveRange:)"
}
-->

# attribute(_:at:effectiveRange:)

Returns the value for an attribute with the specified name of the character at the specified index and, by reference, the range where the attribute applies.

```
func attribute(_ attrName: NSAttributedString.Key, at location: Int, effectiveRange range: NSRangePointer?) -> Any?
```

## Parameters

`attrName`

The name of an attribute.

`location`

The index for which to return attributes. This value must not exceed the bounds of the receiver.
  
  > Important:
  > Raises an ``doc://com.apple.foundation/documentation/Foundation/NSExceptionName/rangeException`` if `index` lies beyond the end of the receiver’s characters.

`range`

If non-`NULL`:

- If the named attribute exists at `index`, upon return `aRange` contains a range over which the named attribute’s value applies.
- If the named attribute does not exist at `index`, upon return `aRange` contains the range over which the attribute does not exist.

    The range isn’t necessarily the maximum range covered by     `attributeName`    , and its extent is implementation-dependent. If you need the maximum range, use [`attribute(_:at:longestEffectiveRange:in:)`](/documentation/Foundation/NSAttributedString/attribute(_:at:longestEffectiveRange:in:))    . If you don’t need this value, pass     `NULL`    .

## Return Value

The value for the attribute named `attrName` of the character at `location`, or `nil` if there is no such attribute.

## Discussion

For a list of possible attributes, see [`NSAttributedString.Key`](/documentation/Foundation/NSAttributedString/Key).

---

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)