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.
相关问题
- NSOutlineView drag line stuck + blue border
- iphone sdk see size of local file (one created
- How can you detect the connection and disconnectio
- How to handle with foreign languages?
- QuickLook Plugin Failing with sandboxing error
相关文章
- Converting (u)int64_t to NSNumbers
- “getter” keyword in @property declaration in Objec
- NSMenuItem KeyEquivalent “ ”(space) bug
- Detect if cursor is hidden on Mac OS X
- NSNumberFormatter doesn't allow typing decimal
- Is subclassing NSNotification the right route if I
- Creating an NSMutableArray with a literal via muta
- adding click action to NSTextField
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.Here's a fully working and clean implementation
To install, use:
To find font glyph names, you have to go to CoreGraphics:
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:
Perhaps -[NSLayoutManager setShowsControlCharacters:] and/or -[NSLayoutManager setShowsInvisibleCharacters:] will do what you want.
Here is Pol's solution in Swift:
And to list the glyph names:
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):