The default implementation returns nil
to indicate that the formatter object does not provide an attributed string.
SDKs
- iOS 2.0+
- macOS 10.0+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
func attributedString(for obj: Any, withDefaultAttributes attrs: [NSAttributed String.Key : Any]? = nil) -> NSAttributed String?
Parameters
anObject
The object for which a textual representation is returned.
attributes
The default attributes to use for the returned attributed string.
Return Value
An attributed string that represents an
.
Discussion
When implementing a subclass, return an NSAttributed
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:)
to get the non-attributed string, then create an NSAttributed
object with it (see 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.