Instance Method


Sets the attributes for the characters in the specified range to the specified attributes.


func setAttributes(_ attrs: [NSAttributedString.Key : Any]?, range: NSRange)



A dictionary containing the attributes to set. Attribute keys can be supplied by another framework or can be custom ones you define. For information about the system-supplied attribute keys, see the Constants section in NSAttributedString.


The range of characters whose attributes are set.


These new attributes replace any attributes previously associated with the characters in aRange. Raises an rangeException if any part of aRange lies beyond the end of the receiver’s characters.

To set attributes for a zero-length NSMutableAttributedString displayed in a text view, use the NSTextView method typingAttributes.

See Also

Changing attributes

func addAttribute(NSAttributedString.Key, value: Any, range: NSRange)

Adds an attribute with the given name and value to the characters in the specified range.

func addAttributes([NSAttributedString.Key : Any], range: NSRange)

Adds the given collection of attributes to the characters in the specified range.

func removeAttribute(NSAttributedString.Key, range: NSRange)

Removes the named attribute from the characters in the specified range.

func applyFontTraits(NSFontTraitMask, range: NSRange)

Applies the font attributes specified by mask to the characters in aRange.

func setAlignment(NSTextAlignment, range: NSRange)

Sets the alignment characteristic of the paragraph style attribute for the characters in aRange to alignment.

func setBaseWritingDirection(NSWritingDirection, range: NSRange)

Sets the base writing direction for the characters in range to writingDirection.

func subscriptRange(NSRange)

Decrements the value of the superscript attribute for characters in aRange by 1.

func superscriptRange(NSRange)

Increments the value of the superscript attribute for characters in aRange by 1.

func unscriptRange(NSRange)

Removes the superscript attribute from the characters in aRange.

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