I took a deep dive into TextKit and wondered how the editor placeholders are implemented in the Xcode code editor:
You can also try this yourself and type something along the lines of: <#Hello#>
, which automatically turns into a placeholder.
The Xcode editor is built with TextKit. After some research I came up with two possible strategies:
NSTextAttachment
: as soon as a string matching the placeholder pattern <#...#>
is detected, that string is removed and replaced by a NSTextAttachment
, which handles drawing the "badge-shaped" background and the text.NSLayoutManager
: not sure if this would work, but it could go like this:
<#
+ #>
are hidden (layoutManager(... shouldGenerateGlyphs, forGlyphRange...)
)drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: NSPoint)
I'd like to implement something similar and would appreciate any suggestions, as to which way to proceed.