Search code examples
swiftuiuikitrich-text-editoruiviewrepresentable

swftUI UIViewRepresentable transform deleted text in strikethrough with custom tag


i am building a custom text editor. I would like when I delete some text, not delete it but transform it into a text with the strikethrough attribute but through a tag not visible in the editor.

Example:
Lorem ipsum dolor sit amet. Vestibulum fermentum finibus libero et tincidunt
Lorem ipsum dolor sit amet. <del id="1">Vestibulum fermentum finibus libero et tincidunt</del>

the tag will be kept, but it will not be visible in the editor and the text inside will be visible as a strikethrough. In short, the typical operation of a rich text editor with the addition of NOT deleting the text, but inserting the text selected with the "del" key inside a tag.

I would also like to be able to keep an ID in the <del tag type <del id="1">

I don't want to use any libraries but I would like to build this editor from scratch.

I started from a UIViewRepresentable and I can add the styles by taking the range of characters (with a regex) to be modified add the necessary attribute
textView.addAttributes (attributes, range: matchRange)

but what I can't understand is:

  1. how to hide the tag in the editor while keeping it active
  2. how to turn the del key into a selection shift to the left?

some idea? Thank you


Solution

  • What you probably want to do is add a custom attribute to the range of text. Your custom attribute would contain your “tag” info (instead of it being added as text). You would also apply strikethrough to the range, as I think you’ve been able to do. See Create custom NSAttributedString.Key for an example.