我有一个 NSTextField,我想让用户有机会使空白、回车符和制表符等不可见字符可见。不幸的是,我在苹果的文档中没有找到关于此的任何文字。我认为我在查找时没有使用正确的术语。
有什么线索可以做到这一点吗?
最佳答案
首先,我会选择 NStextView,其中已经为您设置了关联的 NSLayoutManager 和 NSTextStorage 组件。然后,您可以通过执行以下步骤来实现您想要做的事情:
子类 NSATSTypesetter 为您的任何字符绘制自定义字形 想要通过重写:
- (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin
<p></p> <p>- (NSRect)boundingBoxForControlGlyphAtIndex:(NSUInteger)glyphIndex forTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(NSRect)proposedRect glyphPosition:(NSPoint)glyphPosition characterIndex:(NSUInteger)charIndex </p>
<li><p>Subclass NSLayoutManager and set its type setter with the above one.
Then override:</p></li>
<p>-(void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin
{
[self.typesetter drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];</p>
[super drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];
}
将 NSTextView 的布局管理器替换为上面的布局管理器:
[[textView textContainer]replaceLayoutManager:[[[MyLayoutManager alloc] init] autorelease]];
基本上,您必须检查 NSLayoutManager 和 NSATSTypesetter 类以获取与文本自定义绘图相关的任何内容。还有关于这一切的详细指南 here 。
关于objective-c - 使 NSTextField 中不可见的字符可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12712288/