TextKit: How is the editor placeholder feature implemented in Xcode?

时间秒杀一切 提交于 2019-12-09 07:03:34

问题


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:

  1. Using 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.
  2. Using NSLayoutManager: not sure if this would work, but it could go like this:
    • The start and end markers (glyphs) <# + #> are hidden (layoutManager(... shouldGenerateGlyphs, forGlyphRange...))
    • The layout manager handles drawing the badge background in 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.

来源:https://stackoverflow.com/questions/53415525/textkit-how-is-the-editor-placeholder-feature-implemented-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!