Available links in custom NSCell

I have a custom NSCell with various elements inside it (images, various text fragments), and one of these text blocks can have various interactive links inside it. I have an NSAttributedString that correctly identifies links and colors them blue, but I cannot figure out how to make the cursor turn into a hand and let the user actually click on them.

Right now, I have a row bound to a cell that is clearly not clickable, but I'm not sure how to add it in any other way, since NSCell does not inherit NSView. Usually I just add an NSTextField as a sub, but in this case I cannot do it.

Any thoughts?

+4
source share
2 answers

The only solution I can think of is to manually test and track the mouse in your NSCell. The hardest part (to which I have no answer) is how to determine the direct text of the link ... I hope someone can answer this?

After you learn about the direct text of the URL, you can implement the click by executing hitTestForEvent. I think you will do it something like this:

// If the event is a mouse down event and the point is inside the rect trigger the url - (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)frame ofView:(NSView *)controlView { NSPoint point = [controlView convertPoint:[event locationInWindow] fromView:nil]; // Check that the point is over the url region if (NSPointInRect(point, urlFrame)) { // If event is mousedown activate url // Insert code here to activate url return NSCellHitTrackableArea; } else { return [super hitTestForEvent:event inRect:frame ofView:controlView]; } } 
+2
source

Based on conversations with Ira Cook and other people, I decided to go for the following solution:

  • Draw directly on NSCell
  • When the mouse enters NSCell, I will immediately add the custom subset of NSView to the NSTableView at the same position as the NSCell that was hanging.
  • Their designs correspond to pixel for pixel, so there is no noticeable difference.
  • This NSView will have an NSTextView (or Field, did not decide), which displays an attributed string with links in it, allowing it to be clickable.
  • When you exit NSCell, its NSView mirror is destroyed

If everything goes according to plan, I should only have 1 NSView attached to the NSTableView, and in most cases it doesn't exist at all. I will be back and tell about my results as soon as I get his job.

+2
source

All Articles