Search code examples
objective-cmacoscocoansbutton

NSTextView overlapping NSButton and making it unclickable


I'm creating a custom NSButton, and since I'm subviewing it with a gradient view, the default textView disappears. In order to have also custom styling on the textView, I've created a custom one and subviewed that too.

The button now looks like:

enter image description here

Which is perfect. But when I click on it, the NSButtonCell isn't triggering the highlight method (only if I click outside the textView (there's 5px spacing on bottom and top)

How can I (just like in iOS's UIButton add a custom label) and still keeping the whole button clickable.

Any help would be appreciated!

Sjors


Solution

  • An NSTextField is a control and therefore captures input events. You shouldn't use it to display the text in a button. NSButton uses NSCell for its drawing so it's best to subclass NSButtonCell and manually draw the string with NSStringDrawing, but only if you really can't use the standard layout properties of NSButtonCell. This isn't iOS, where there is something like UILabel as a lightweight solution to draw text. Instead, NSTextField provides a lot of overhead and would slow down your application and may provide you with unnecessary bugs, now or in the future.

    Tl;dr: Don't use NSTextField, draw the string yourself.