1

I am trying to draw some text that is clickable. I am trying not to use NSButton as my text is being drawn in drawRect and using NSButton in a drawRect makes it add the subview on top of itself unless you go through and remove all subviews on each call of drawRect.

I tried creating a NSButton and instead of adding it as a subview I am calling [myBtn.cell drawTitle: withFrame: inView:] which draws the text, but it is not clickable.

Is there a way to draw text (in a way like using drawInRect) but where I can also have the text be clickable?

If not, what would you recommend? Using a standard NSButton and clearing all the subviews each time?

kdbdallas
  • 4,513
  • 10
  • 38
  • 53

1 Answers1

2

One way I've seen this done before is to use an NSAttributedString. The part that's clickable should be changed somehow (ie, have a different NSForegroundColorAttributeName and NSUnderlineStyleAttributeName) and then attach the NSURL to it with the NSLinkAttributeName.

If I remember correctly, I believe this is an NSTextField subclass that does this: DSClickableURLTextField / Swift DSClickableURLTextField fork

I would recommend not drawing the text yourself, because there are lots of things in AppKit that will do this for you. Why do you need to draw it yourself?

catlan
  • 25,100
  • 8
  • 67
  • 78
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Main reason for drawing myself is the fact that if you add a subview in a drawRect then you have to clear the subviews on each run of drawRect or you get multiple subviews on top of each other. I guess it comes down to that I probably shouldn't be doing my text/labels in drawRect.... That is probably the "correct" solution.... – kdbdallas Jun 27 '11 at 20:19
  • @kdbdallas my general rule is that if something else can draw it for you, let it. :) – Dave DeLong Jun 27 '11 at 20:22