在 OS X 上的 Cocoa 中实现自定义 NSView

发布于 2024-10-11 03:07:35 字数 415 浏览 8 评论 0原文

我正在创建一个 Cocoa 应用程序,其中一个视图将包含当前加载文档的“十六进制转储”。到目前为止,我一直在使用沼泽标准(并且非常没有吸引力)NSTextField,但我现在正在寻找更强大的东西。

我是 0xED.app 的忠实粉丝,并且会喜欢复制其主要的“十六进制转储”视图。我该怎么做呢?

我不一定追求养眼的效果,而是能够选择一系列字节而无需选择偏移量或文本列。我什至不知道从哪里开始有效地实施这一点。这肯定不是在空白画布上绘制的吗?

0xED 截图

I am creating a Cocoa application wherein one view will contain the "hex dump" of the currently loaded document. Up until this point, I have been using a bog-standard (and very unappealing) NSTextField, but I am now looking for something more powerful.

I am a great fan of 0xED.app and would love to replicate its main "hex dump" view. How would I go about doing this?

I'm not necessarily after the eye-candy, but the ability to select a range of bytes without also selecting the offset or text columns. I am a loss as to where I would even begin to implement this effectively. Surely this is not drawn upon a blank canvas?

0xED screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

維他命╮ 2024-10-18 03:07:35

要开始并了解事情的基本工作原理:

  1. 子类 NSView。
  2. 添加一个实例变量来保存您的 NSData。
  3. 重写绘制矩形:

对于大量数据,这种方法会很慢,但可以让您很好地实现 NSView 子类。之后,您将希望通过实现比重复调用一次绘制一个字符串更好的方法来提高绘制性能。您还需要实现 mouseDown: 和 keyDown: 等方法的重写,以处理用户的输入,以允许选择字节范围等操作。

这应该可以帮助您开始,一旦您开始这样做,我建议您提出后续问题。

To get started and see how things basically work:

  1. Subclass NSView.
  2. Add an instance variable to hold your NSData.
  3. Override drawRect:

This approach will be slow for a large amount of data, but will give you a good handle on implementing a NSView subclass. After that, you'll want to improve the drawing performance by implementing something better than repeated calls to draw strings one at a time. You'll also want to implement overrides of methods like mouseDown: and keyDown: to handle input from the user to allow things like selecting a range of bytes.

That should get you started, once you have that going, I'd suggest asking follow up questions.

北城挽邺 2024-10-18 03:07:35

我的猜测是,它可能是使用 NSTableView 或其子类。

以这种方式完成正确的文本选择可能有点棘手,但这可能是可能的。

My guess is that it's probably accomplished using a NSTableView or subclass of it.

It might be a little tricky to get the correct text selection accomplished this way, but it's probably possible.

只有影子陪我不离不弃 2024-10-18 03:07:35

如果你想看看 Cocoa 界面是如何构建的,你可以使用 NibToXibConverter

  1. 下载 0xED,右键单击 0xED.app 并选择“显示包内容”。提取 Contents/Resources/English.lproj 文件夹。
  2. 运行 NibToXibConverter,浏览到上面提取的文件夹,然后在“反编译 NIB”旁边打勾。
  3. 选择“转换”按钮,它将把 NIB 转换为 XIB
  4. 双击 XIB 在 XCode 中打开它,您可以看到它们是如何构造的

    • 您会注意到,在 0xED 的情况下,他使用的是自定义类(很可能是 UIView 的子类,如 Jon Hess 所建议的那样,具有自定义绘图)。

If you want to take a look at how a Cocoa interface is built you can use NibToXibConverter.

  1. Download 0xED, right click on 0xED.app and select "Show Package Contents". Extract the Contents/Resources/English.lproj folder.
  2. Run NibToXibConverter, browse to the folder extracted above, and put a tick next to "Decompile NIBs".
  3. Select the "Convert" button and it will convert the NIBs to XIBs
  4. Double click a XIB to open it in XCode and you can see how they are constructed

    • You will note that in the case of 0xED he is using a custom class (most likely a subclass of UIView with custom drawing as Jon Hess suggests).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文