objective-c - 使 NSTextField 中不可见的字符可见

标签 objective-c cocoa nstextfield

我有一个 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/

相关文章:

ios - 如何在iPhone SDK中合并音频和视频

ios - 我可以在 iOS 中连接摩托罗拉 rs507 作为蓝牙外设吗?

ios - Facebook 登录后更改 View Controller

Swift:如何从 NSTextField 连续发送一个 Action

objective-c - 将 objc_setAssociatedObject 与弱引用一起使用

objective-c - 哪个方法名称最符合 Objective-C/Cocoa 约定?

objective-c - NSTableColumn 何时被释放?

iphone - 更改 NSIndexPath 对象的值

cocoa - 使 NSWindow.isMovableByWindowBackground 与 NSTextField 一起使用

objective-c - 自动布局中两个具有相互依赖宽度的 NSTextField