I have an NSTextView based application where I have various commands changing the text. In order to replace a portion I use the following snippet:
if ([textView shouldChangeTextInRange:range replacementString:string]) { //string
[textStorage beginEditing];
[textStorage replaceCharactersInRange:range withAttributedString:attributedString];
[textStorage endEditing];
[textView didChangeText];
}
My issue starts when I want to append at the end of the textview. In that case I cannot use shouldChangeTextInRange:range replacementString:string so I use :
textView.textStorage?.beginEditing()
self.textView.textStorage?.append(NSAttributedString(string: string))
self.textView.scrollToEndOfDocument(nil)
textView.textStorage?.endEditing()
textView.didChangeText()
While it works fine for attributes, Undo totally breaks. Any idea how I can fix that issue?