开发者

Implementing custom NSView in Cocoa on OS X

开发者 https://www.devze.com 2023-02-03 07:37 出处:网络
I am creating a Cocoa application wherein one view will contain the \"hex dump\" of the currently loaded document. Up until this point, I ha开发者_如何学运维ve been using a bog-standard (and very unap

I am creating a Cocoa application wherein one view will contain the "hex dump" of the currently loaded document. Up until this point, I ha开发者_如何学运维ve 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?

Implementing custom NSView in Cocoa on OS X


To get started and see how things basically work:

  1. Subclass NSView.
  2. Add an instance variable to hold your NSData.
  3. Override drawRect:
    • Iterate your NSData
    • Use methods from NSString drawing AppKit additions to draw the bytes.

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.


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.


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).
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号