Display hidden characters in NSTextView

前端 未结 6 1573
孤城傲影
孤城傲影 2020-12-15 01:14

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 se

6条回答
  •  误落风尘
    2020-12-15 02:09

    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];
    }

提交回复
热议问题