Display hidden characters in NSTextView

久未见 提交于 2019-11-27 14:10:52

问题


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.


回答1:


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.




回答2:


Here's a fully working and clean implementation

@interface GILayoutManager : NSLayoutManager
@end

@implementation GILayoutManager

- (void)drawGlyphsForGlyphRange:(NSRange)range atPoint:(NSPoint)point {
  NSTextStorage* storage = self.textStorage;
  NSString* string = storage.string;
  for (NSUInteger glyphIndex = range.location; glyphIndex < range.location + range.length; glyphIndex++) {
    NSUInteger characterIndex = [self characterIndexForGlyphAtIndex: glyphIndex];
    switch ([string characterAtIndex:characterIndex]) {

      case ' ': {
        NSFont* font = [storage attribute:NSFontAttributeName atIndex:characterIndex effectiveRange:NULL];
        [self replaceGlyphAtIndex:glyphIndex withGlyph:[font glyphWithName:@"periodcentered"]];
        break;
      }

      case '\n': {
        NSFont* font = [storage attribute:NSFontAttributeName atIndex:characterIndex effectiveRange:NULL];
        [self replaceGlyphAtIndex:glyphIndex withGlyph:[font glyphWithName:@"carriagereturn"]];
        break;
      }

    }
  }

  [super drawGlyphsForGlyphRange:range atPoint:point];
}

@end

To install, use:

[myTextView.textContainer replaceLayoutManager:[[GILayoutManager alloc] init]];

To find font glyph names, you have to go to CoreGraphics:

CGFontRef font = CGFontCreateWithFontName(CFSTR("Menlo-Regular"));
for (size_t i = 0; i < CGFontGetNumberOfGlyphs(font); ++i) {
  printf("%s\n", [CFBridgingRelease(CGFontCopyGlyphNameForGlyph(font, i)) UTF8String]);
}



回答3:


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):

- (void)drawGlyphsForGlyphRange:(NSRange)glyphRange atPoint:(NSPoint)containerOrigin
{
    if ([[[[MJDocumentController sharedDocumentController] currentDocument] editor] showInvisibles])
    {
        //init glyphs
        unichar crlf = 0x00B6; 
        NSString *CRLF = [[NSString alloc] initWithCharacters:&crlf length:1];
        unichar space = 0x00B7;
        NSString *SPACE = [[NSString alloc] initWithCharacters:&space length:1];
        unichar tab = 0x2192; 
        NSString *TAB = [[NSString alloc] initWithCharacters:&tab length:1];

        NSString *docContents = [[self textStorage] string];
        NSString *glyph;
        NSPoint glyphPoint;
        NSRect glyphRect;
        NSDictionary *attr = [[NSDictionary alloc] initWithObjectsAndKeys:[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"invisiblesColor"]], NSForegroundColorAttributeName, nil];

        //loop thru current range, drawing glyphs
        int i;
        for (i = glyphRange.location; i < NSMaxRange(glyphRange); i++)
        {
            glyph = @"";

            //look for special chars
            switch ([docContents characterAtIndex:i])
            {
                //space
                case ' ':
                    glyph = SPACE;
                    break;

                //tab
                case '\t':
                    glyph = TAB;
                    break;

                //eol
                case 0x2028:
                case 0x2029:
                case '\n':
                case '\r':
                    glyph = CRLF;
                    break;

                //do nothing
                default:
                    glyph = @"";
                    break;                  
            }

            //should we draw?
            if ([glyph length])
            {
                glyphPoint = [self locationForGlyphAtIndex:i];
                glyphRect = [self lineFragmentRectForGlyphAtIndex:i effectiveRange:NULL];
                glyphPoint.x += glyphRect.origin.x;
                glyphPoint.y = glyphRect.origin.y;
                [glyph drawAtPoint:glyphPoint withAttributes:attr];
            }
        }
    }

    [super drawGlyphsForGlyphRange:glyphRange atPoint:containerOrigin];
}



回答4:


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:

- (void)drawGlyphsForGlyphRange:(NSRange)range atPoint:(NSPoint)origin
{
    NSFont *font = [[CURRENT_TEXT_VIEW typingAttributes]
                       objectForKey:NSFontAttributeName];

    NSGlyph bullet = [font glyphWithName:@"bullet"];

    for (int i = range.location; i != range.location + range.length; i++)
    {
        unsigned charIndex = [self characterIndexForGlyphAtIndex:i];

        unichar c =[[[self textStorage] string] characterAtIndex:charIndex];

        if (c == ' ')
            [self replaceGlyphAtIndex:charIndex withGlyph:bullet];
    }

    [super drawGlyphsForGlyphRange:range atPoint:origin];
}



回答5:


Perhaps -[NSLayoutManager setShowsControlCharacters:] and/or -[NSLayoutManager setShowsInvisibleCharacters:] will do what you want.




回答6:


Here is Pol's solution in Swift:

class MyLayoutManager: NSLayoutManager {
    override func drawGlyphsForGlyphRange(glyphsToShow: NSRange, atPoint origin: NSPoint) {
        if let storage = self.textStorage {
            let s = storage.string
            let startIndex = s.startIndex
            for var glyphIndex = glyphsToShow.location; glyphIndex < glyphsToShow.location + glyphsToShow.length; glyphIndex++ {
                let characterIndex = self.characterIndexForGlyphAtIndex(glyphIndex)
                let ch = s[startIndex.advancedBy(characterIndex)]
                switch ch {
                case " ":
                    let attrs = storage.attributesAtIndex(characterIndex, effectiveRange: nil)
                    if let font = attrs[NSFontAttributeName] {
                        let g = font.glyphWithName("periodcentered")
                        self.replaceGlyphAtIndex(glyphIndex, withGlyph: g)
                    }
                case "\n":
                    let attrs = storage.attributesAtIndex(characterIndex, effectiveRange: nil)
                    if let font = attrs[NSFontAttributeName] {
//                        let g = font.glyphWithName("carriagereturn")
                        let g = font.glyphWithName("paragraph")
                        self.replaceGlyphAtIndex(glyphIndex, withGlyph: g)
                    }
                case "\t":
                    let attrs = storage.attributesAtIndex(characterIndex, effectiveRange: nil)
                    if let font = attrs[NSFontAttributeName] {
                        let g = font.glyphWithName("arrowdblright")
                        self.replaceGlyphAtIndex(glyphIndex, withGlyph: g)
                    }
                default:
                    break
                }
            }
        }
        super.drawGlyphsForGlyphRange(glyphsToShow, atPoint: origin)
    }
}

And to list the glyph names:

   func listFonts() {
        let font = CGFontCreateWithFontName("Menlo-Regular")
        for var i:UInt16 = 0; i < UInt16(CGFontGetNumberOfGlyphs(font)); i++ {
            if let name = CGFontCopyGlyphNameForGlyph(font, i) {
                print("name: \(name) at index \(i)")
            }
        }
    }


来源:https://stackoverflow.com/questions/300086/display-hidden-characters-in-nstextview

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