Which replacement method is called when editing an

2019-07-26 05:08发布

问题:

NSMutableAttributedString defines two string replacement methods:

func replaceCharacters(in range: NSRange, 
                       with str: String)

and

func replaceCharacters(in range: NSRange, 
                with attrString: NSAttributedString)

I have created a subclass of NSTextStorage which in its turn is a subclass of NSMutableAttributedString. In that subclass I overrode both of the above methods.

To my surprise, the latter method (which passes an attributed replacement string) is never called when I type or paste something in my text view. It's only the plain string replacement method that gets called each time.

This leads me to the question:
Based on which rules are these two methods actually called, when the user is editing the text in a text view?

(I need to perform different operations in my text storage, depending on whether the attributed replacement string contains a specific kind of text attachment. But if the replacement method for the attributed string is never called, I see no way how to make this distinction.