在 NSTextView 中显示隐藏字符
我正在为 Mac OS X 编写一个文本编辑器。我需要在 NSTextView 中显示隐藏字符(例如空格、制表符和特殊字符)。 我花了很多时间寻找如何做到这一点,但到目前为止我还没有找到答案。 如果有人能指出我正确的方向,我将不胜感激。
I am writing a text editor for Mac OS X. I need to display hidden characters in an NSTextView (such as spaces, tabs, and special characters). I have spent a lot of time searching for how to do this but so far I have not found an answer. If anyone could point me in the right direction I would be grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一个完全有效且干净的实现
要安装,请使用:
要查找字体字形名称,您必须转到 CoreGraphics:
Here's a fully working and clean implementation
To install, use:
To find font glyph names, you have to go to CoreGraphics:
看一下 NSLayoutManager 类。 你的 NSTextView 将有一个与之关联的布局管理器,布局管理器负责将字符(空格、制表符等)与字形(在屏幕上绘制的该字符的图像)相关联。
就您而言,您可能对
replaceGlyphAtIndex:withGlyph:
方法,该方法允许您替换单个字形。Have a look at the NSLayoutManager class. Your NSTextView will have a layout manager associated with it, and the layout manager is responsible for associating a character (space, tab, etc.) with a glyph (the image of that character drawn on the screen).
In your case, you would probably be most interested in the
replaceGlyphAtIndex:withGlyph:
method, which would allow you to replace individual glyphs.几年前我写了一个文本编辑器 - 这里有一些毫无意义的代码,应该能让你(希望)找到正确的方向(顺便说一句,这是一个 NSLayoutManager 子类 - 是的,我知道它像众所周知的厨房水槽一样泄漏):
I wrote a text editor a few years back - here's some meaningless code that should get you looking in (hopefully) the right direction (this is an NSLayoutManager subclass btw - and yes I know it's leaking like the proverbial kitchen sink):
我解决了NSGlyphs和NSTextView中相应的unichar之间转换的问题。 下面的代码工作得很好,并用项目符号替换了可见文本的空格:
I solved the problem of converting between NSGlyphs and the corresponding unichar in the NSTextView. The code below works beautifully and replaces spaces with bullets for visible text:
也许 -[NSLayoutManager setShowsControlCharacters:] 和/或 -[NSLayoutManager setShowsInvisibleCharacters:] 会做你想要的。
Perhaps -[NSLayoutManager setShowsControlCharacters:] and/or -[NSLayoutManager setShowsInvisibleCharacters:] will do what you want.
这是 Pol 在 Swift 中的解决方案:
并列出字形名称:
Here is Pol's solution in Swift:
And to list the glyph names: