Custom paste into UITextView

I am implementing a UITextView subclass that supports the editing of structured text with block styles (e.g. paragraph, heading, list, quote etc.) and inline styles (e.g. emphasis, code etc.)

I need to be able to cut, copy and paste structured text from the UITextView. Implementing this is straightforward: override cut(_:), copy(_:) and paste(_:) and read/write the structured text (a JSON format) to/from the general pasteboard.

This works fine, but the UITextView's smart insert/delete behavior (see UITextInputTraits.smartInsertDeleteType), which adjusts spaces around the selection, is lost.

I have explored implementing a custom text paste delegate (UITextPasteDelegate). This would appear to provide support for custom paste behaviour. I can to extract the contents of the pasteboard and transform them to an attributed string for pasting by implementing textPasteConfigurationSupporting(_:, transform:) however any custom attributes I add to the string placed in the UITextPasteItem are ignored (standard attributes are also ignored because allowsEditingTextAttributes is set to false).

I can solve this issue by implementing textPasteConfigurationSupporting(_:, performPasteOf:, to:) but this breaks the smart insert/delete behaviour, even when pasting plain text. Unfortunately I can see no way to paste custom textual data into a UITextView without losing the standard smart insert/delete behaviour.

Am I missing something? Any pointers on how I can achieve cut/copy/paste for a custom clipboard format while maintaining smart insert/delete would be appreciated.

Custom paste into UITextView
 
 
Q