How to replace multiple glyphs in Swift NS/UITextView with a single icon

Viewed 101

I'm struggling with this one for a while now. Its's about rendering markdown text in UI/NSTextView.

In the app "Bear" for Mac/iOS you can add a markdown link like [This is a link](https://...com) and it will hide the full URL and show a link symbol instead.

Here is a screenshot:

enter image description here

I know how to change colors using NSAttributedString and I know how to hide glyphs by overriding NSLayoutManagerDelegate.

There I override the shouldGenerateGlyphs func and change the properties of the glyphs to .null to hide them. Here I can also 'replace' a single glyph with something else by creating a CGGlyph(exactly: Int) and setting the glyph pointer to this in the same method where you can hide glyphs. But you can only replace a single glyph, not multiple.

While the above might work, it feels very hacky. I would need to hide all URL string glyphs, except the first, and turn that into a symbol somehow.

Is there any way to say something in the sense of mergeGlyphs(in range: NSRange, to glyph: CGGlyph) or to combine multiple glyphs to one or any other idea this could be achieved?

0 Answers
Related