mss
mss

Reputation: 387

Customizing a NSButtonCell with images and highlighting

I want to create a custom switch for my application, in which I supply three images for the possible states (on, off, mousedown). The whole appearance is contained in the images, so I do not want Cocoa to highlight (darken) the button by itself during mousedown.

At the moment, I have created a NSButtonCell subclass, dragged a bevel button of NSButtonTypeMomentaryChange into my view and custom set the button cell class type to my subclass. The subclass just implements awakeFromNib to try and set some desired behavior:

- (void)awakeFromNib
{
    [self setShowsStateBy:NSContentsCellMask];
    [self setHighlightsBy:NSContentsCellMask];
}

which I thought (according to the documentation) would disable the mousedown-darkening of the button. It does not.

This is where I stand and now I have two questions:

Upvotes: 5

Views: 3753

Answers (1)

unknown
unknown

Reputation: 84

Set your button's type to On/Off. Subclass its cell. Write this two methods:

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    if ([self state])
    {
        [onStateImage drawInRect:cellFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    }
    else
    {
        [offStateImage drawInRect:cellFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    }
} 
- (void)highlight:(BOOL)flag withFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    if (flag){
        [downStateImage drawInRect:cellFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

    }
}

Upvotes: 6

Related Questions