<!--
{
  "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:longestEffectiveRange:in:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSAttributedString(im)attribute:atIndex:longestEffectiveRange:inRange:"
  },
  "title" : "attribute(_:at:longestEffectiveRange:in:)"
}
-->

# attribute(_:at:longestEffectiveRange:in:)

Returns the value for the 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, longestEffectiveRange range: NSRangePointer?, in rangeLimit: NSRange) -> Any?
```

## Parameters

`attrName`

The name of an attribute.

`location`

The index at which to test for `attributeName`.

`range`

If non-`NULL`:

- If the named attribute exists at `index`, upon return `aRange` contains the full range over which the value of the named attribute is the same as that at `index`, clipped to `rangeLimit`.
- If the named attribute does not exist at `index`, upon return `aRange` contains the full range over which the attribute does not exist, clipped to `rangeLimit`.

    If you don’t need this value, pass     `NULL`    .

`rangeLimit`

The range over which to search for continuous presence of `attributeName`. This value must not exceed the bounds of the receiver.

## Return Value

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

## Discussion

Raises an [`rangeException`](/documentation/Foundation/NSExceptionName/rangeException) if `index` or any part of `rangeLimit` lies beyond the end of the receiver’s characters.

If you don’t need the longest effective range, it’s far more efficient to use the [`attribute(_:at:effectiveRange:)`](/documentation/Foundation/NSAttributedString/attribute(_:at:effectiveRange:)) method to retrieve the attribute value.

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)