Показать скрытые символы в NSTextView - PullRequest
6 голосов
/ 18 ноября 2008

Я пишу текстовый редактор для Mac OS X. Мне нужно отобразить скрытые символы в NSTextView (например, пробелы, табуляции и специальные символы). Я потратил много времени на поиски того, как это сделать, но до сих пор не нашел ответа. Если бы кто-нибудь мог указать мне правильное направление, я был бы благодарен.

Ответы [ 6 ]

8 голосов
/ 16 апреля 2015

Вот полностью рабочая и чистая реализация

@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

Для установки используйте:

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

Чтобы найти имена глифов шрифтов, вам нужно перейти в CoreGraphics:

CGFontRef font = CGFontCreateWithFontName(CFSTR("Menlo-Regular"));
for (size_t i = 0; i < CGFontGetNumberOfGlyphs(font); ++i) {
  printf("%s\n", [CFBridgingRelease(CGFontCopyGlyphNameForGlyph(font, i)) UTF8String]);
}
4 голосов
/ 19 ноября 2008

Посмотрите на класс NSLayoutManager. Ваш NSTextView будет иметь связанный с ним менеджер макета, а менеджер макета отвечает за связывание символа (пробел, табуляция и т. Д.) С глифом (изображение этого символа, нарисованное на экране).

В вашем случае вас, вероятно, больше всего заинтересует метод <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSLayoutManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSLayoutManager/replaceGlyphAtIndex:withGlyph:" rel="nofollow noreferrer">replaceGlyphAtIndex:withGlyph:</a>, который позволит вам заменять отдельные глифы.

3 голосов
/ 23 февраля 2009

Я написал текстовый редактор несколько лет назад - вот какой-то бессмысленный код, который должен заставить вас смотреть (надеюсь) в правильном направлении (кстати, это подкласс NSLayoutManager - и да, я знаю, что он протекает как общеизвестная раковина):

- (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];
}
2 голосов
/ 23 февраля 2009

Возможно - [NSLayoutManager setShowsControlCharacters:] и / или - [NSLayoutManager setShowsInvisibleCharacters:] будет делать то, что вы хотите.

2 голосов
/ 19 ноября 2008

Я решил проблему преобразования между NSGlyphs и соответствующим unichar в NSTextView. Приведенный ниже код прекрасно работает и заменяет пробелы для видимого текста:

- (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];
}
0 голосов
/ 24 ноября 2015

Вот решение Пола в 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)
    }
}

И перечислить имена глифов:

   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)")
            }
        }
    }
...