gpt4 book ai didi

cocoa - 在 NSTextView 中显示隐藏字符

转载 作者:行者123 更新时间:2023-12-03 16:08:10 25 4
gpt4 key购买 nike

我正在为 Mac OS X 编写一个文本编辑器。我需要在 NSTextView 中显示隐藏字符(例如空格、制表符和特殊字符)。我花了很多时间寻找如何做到这一点,但到目前为止我还没有找到答案。如果有人能指出我正确的方向,我将不胜感激。

最佳答案

这是一个完全有效且干净的实现

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

关于cocoa - 在 NSTextView 中显示隐藏字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/300086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com