在NSTextField中显示不可见的字符

时间:2012-10-03 15:57:15

标签: objective-c cocoa nstextfield

我有一个NSTextField,我想让用户有机会制作不可见的字符,如空格,回车和标签可见。不幸的是,我没有在Apple的文档中找到关于此的话。我认为在寻找时我没有使用正确的术语。

任何线索如何做到这一点?

1 个答案:

答案 0 :(得分:1)

首先,我会选择NStextView,其中已经为您设置了关联的NSLayoutManager和NSTextStorage组件。然后,您可以通过执行以下步骤来实现您要执行的操作:

  • 子类NSATSTypesetter为您的任何字符绘制自定义字形 想要重写:

    - (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin

    - (NSRect)boundingBoxForControlGlyphAtIndex:(NSUInteger)glyphIndex forTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(NSRect)proposedRect glyphPosition:(NSPoint)glyphPosition characterIndex:(NSUInteger)charIndex

  • Subclass NSLayoutManager and set its type setter with the above one. Then override:

-(void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(NSPoint)origin { [self.typesetter drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];

[super drawGlyphsForGlyphRange:glyphsToShow atPoint:origin];

}

  • 用上面的替换NSTextView的布局管理器:

    [[textView textContainer] replaceLayoutManager:[[[MyLayoutManager alloc] init] autorelease]];

基本上,您必须检查NSLayoutManager和NSATSTypesetter类以查找与文本自定义绘图相关的任何内容。还有关于所有这些here的详细指南。