Question

In Interface Builder components I have Gradient Button

Gradient Button

I would like to create this button programmatically. But I can't find combination of button type, bezel style and gradient type to reproduce it. My local goal is to create plus (add) and minus (remove) buttons like this

Add and remove items, gradient button

May be you can also know how to implement part at right of minus? It looks like disabled gradient button.

Update (2012-02-01, 14:52)

I put gradient button on empty xib, open xib with text editor and find my button. This is a part of code.

<array class="NSMutableArray" key="NSSubviews">
  <object class="NSButton" id="155381693">
    <reference key="NSNextResponder" ref="1005"/>
    <int key="NSvFlags">268</int>
    <string key="NSFrame">{{20, 303}, {106, 23}}</string>
    <reference key="NSSuperview" ref="1005"/>
    <reference key="NSWindow"/>
    <string key="NSReuseIdentifierKey">_NS:2466</string>
    <bool key="NSEnabled">YES</bool>
    <object class="NSButtonCell" key="NSCell" id="976550657">
      <int key="NSCellFlags">-2080244224</int>
      <int key="NSCellFlags2">134217728</int>
      <string key="NSContents">Gradient Button</string>
      <object class="NSFont" key="NSSupport">
        <string key="NSName">LucidaGrande</string>
        <double key="NSSize">13</double>
        <int key="NSfFlags">1044</int>
      </object>
      <string key="NSCellIdentifier">_NS:2466</string>
      <reference key="NSControlView" ref="155381693"/>
      <int key="NSButtonFlags">-2033434369</int>
      <int key="NSButtonFlags2">162</int>
      <string key="NSAlternateContents"/>
      <string key="NSKeyEquivalent"/>
      <int key="NSPeriodicDelay">400</int>
      <int key="NSPeriodicInterval">75</int>
    </object>
  </object>
</array>

I will try to reproduce button using this information.

Was it helpful?

Solution

OMG, I was blind. Just set bezel style to NSSmallSquareBezelStyle and button type to NSMomentaryPushInButton.

I don't need images. I don't need custom draw with CoreGraphics (I did my own class for that). I don't need to parse xib. I don't need to dump runtime structures (I did it to find nessesuary values of fields in outlet to gradient button created with xib). I had to try all values of bezel style from documentation. But if you read description for NSSmallSquareBezelStyle in documentation you understand my confusion: it's very far from "Gradient" value in IB wizard.

enter image description here

Correct code:

NSButton *gradientButton = [[NSButton alloc] initWithFrame:NSMakeRect(40, 40, 80, 32)];
[gradientButton setButtonType:NSMomentaryPushInButton];
[gradientButton setBezelStyle:NSSmallSquareBezelStyle];
[gradientButton setTitle:@"Title"];
[self.window.contentView addSubview:gradientButton];

Result:

enter image description here

OTHER TIPS

I believe the button you are referencing is a custom button and probably a custom background image, although it could certainly be programmatically created. The problem with these types of buttons is that there have been several variants of that button which all look very similar but have slight differences.

The easiest way to create those buttons is to use the gradient buttons and drag the + and - images from Xcodes stock icons onto them. Otherwise you may want to look around for a custom NSButton that looks like what you want. I believe there have been some open sourced programs that have these, but I can't remember them right off the top of my head.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top