How do you create a custom themed NSButton? I don't mean in a small way like changing the background color or changing from rounded edges to square edges. I want to replace the entire look and feel of the button. Is that even possible to do in Cocoa? Obviously I would have to subclass the NSButton class and go from there. Any help would be much appreciated.
+5
A:
Actually, you need to subclass NSButtonCell
. You should read Apple's documentation on this to gain a better understanding of how they interact. You probably will still want to subclass NSButton
so that it will use your NSButtonCell
subclass, too.
For a button, most of the work is done in drawBezelWithFrame:inView:
. If you want to alter the way the text or image is drawn, you would override drawText:withFrame:inView:
and drawImage:withFrame:inView:
.
Alex
2009-10-28 15:46:25
Yep, that's the best way to do it.Note, if you're using Interface Builder and Mac OS 10.5 you no longer have to subclass NSButton. Simply drag/drop the button in your nib and then change the cell class to your custom class.
Leibowitzn
2009-10-28 21:15:15
This is true. Any more, the only time you'd really need to subclass `NSButton` is if you were creating the controls programmatically.
Alex
2009-10-29 16:24:53
Can I just want to replace the appearance by setting a property? I was able to change the appearance by using the "image" property of the button but I got an ugly gray rectangle whenever the custom button is clicked (the image has a transparency).Thanks.
adib
2010-07-13 15:10:59