<!--
{
  "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/Formatter/attributedString(for:withDefaultAttributes:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFormatter(im)attributedStringForObjectValue:withDefaultAttributes:"
  },
  "title" : "attributedString(for:withDefaultAttributes:)"
}
-->

# attributedString(for:withDefaultAttributes:)

The default implementation returns `nil` to indicate that the formatter object does not provide an attributed string.

```
func attributedString(for obj: Any, withDefaultAttributes attrs: [NSAttributedString.Key : Any]? = nil) -> NSAttributedString?
```

## Parameters

`obj`

The object for which a textual representation is returned.

`attrs`

The default attributes to use for the returned attributed string.

## Return Value

An attributed string that represents `anObject`.

## Discussion

When implementing a subclass, return an `NSAttributedString` object if the string for display should have some attributes. For instance, you might want negative values in a financial application to appear in red text. Invoke your implementation of [`string(for:)`](/documentation/Foundation/Formatter/string(for:)) to get the non-attributed string, then create an `NSAttributedString` object with it (see [`init(string:)`](/documentation/Foundation/NSAttributedString/init(string:))). Use the `attributes` default dictionary to reset the attributes of the string when a change in value warrants it (for example, a negative value becomes positive) For information on creating attributed strings, see [Attributed String Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/AttributedStrings/AttributedStrings.html#//apple_ref/doc/uid/10000036i).

---

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)