Instance Method


Executes the specified closure for each range of a particular attribute in the attributed string.


func enumerateAttribute(_ attrName: NSAttributedString.Key, in enumerationRange: NSRange, options opts: NSAttributedString.EnumerationOptions = [], using block: (Any?, NSRange, UnsafeMutablePointer<ObjCBool>) -> Void)



The name of the attribute to enumerate.


The range over which the attribute values are enumerated.


The options used by the enumeration. For possible values, see NSAttributedString.EnumerationOptions.


A closure to apply to ranges of the specified attribute in the attributed string.

The closure takes three arguments:

  • The value for the specified attribute.

  • The range of the attribute value in the attributed string.

  • A reference to a Boolean value, which you can set to true within the closure to stop further processing of the attributed string.


If this method is called by an instance of NSMutableAttributedString, mutation (deletion, addition, or change) is allowed only if the mutation is within the range provided to the block. After a mutation, the enumeration continues with the range immediately following the processed range, adjusting for any change in length caused by the mutation. For example, if block is called with a range starting at location N, and the block deletes all the characters in the provided range, the next call will also pass N as the location of the range.

See Also

Enumerating over Attributes in a String