How to change the attributes of only future text in an NSTextStorage?

466 Views Asked by At

Good day,

i'll like to know how I can change the attributes of only future text in an NSTextStorage, setting the attributes of the textStorage changes all the text in the Text Storage not future text. if anyone could assist me with this. it would be highly appreciated.

Thank you

1

There are 1 best solutions below

1
On BEST ANSWER

NSTextStorage is a subclass of NSMutableAttributedString. You can set the attributes for any range of characters using setAttributes(_:range:) and related methods. You can append and insert NSAttributedStrings (with their own attributes) using append() and insert().

There are no "current" attributes in an NSMutableAttributedString. You assign attributes as part of the NSAttributedString you're adding. If you want to change the attributes of newly added text, then you will need to create NSAttributedStrings with those attributes.

If you're working with a (NS|UI)TextView or UITextField, you can use typingAttributes to set what attributes should be applied to new text, but this is handled at the UI layer, not the NSTextStorage layer. If you're making your own UI element, you'll need to handle this yourself.