Instance Method


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?



The object for which a textual representation is returned.


The default attributes to use for the returned attributed string.

Return Value

An attributed string that represents anObject.


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:) to get the non-attributed string, then create an NSAttributedString 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.

See Also

Getting Textual Representations of Object Values

func string(for: Any?) -> String?

The default implementation of this method raises an exception.

func editingString(for: Any) -> String?

The default implementation of this method invokes string(for:).

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software