Variable Width NSButton, Rounded Corners

What is the best way to create an NSButton with a custom background image that can have a variable width without increasing the angle without a border? I know there are convenient methods for doing this with UIButton: http://jainmarket.blogspot.com/2009/04/create-uibuttonbutton-with-images.html , but I did not see anything in NSButton.

+5
source share
4 answers

I needed to create my own button background, this is how I did it. I subclassed NSButton and redid the correct method:

- (void)drawRect:(NSRect)dirtyRect
{
    // My buttons don't have a variable height, so I make sure that the height is fixed
    if (self.frame.size.height != 22) {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width,
                                22.0f);
    }
    // 
    switch (self.state) {
            // Onstate graphics
        case NSOnState:
            NSDrawThreePartImage(self.bounds, 
                                 [NSImage imageNamed:@"btnmain_lb_h.png"], [NSImage imageNamed:@"btnmain_bg_h.png"], [NSImage imageNamed:@"btnmain_rb_h.png"],
                                 NO, NSCompositeSourceAtop, 1.0, NO);
            // Offstate graphics
        default:
        case NSOffState:
            NSDrawThreePartImage(self.bounds, 
                                 [NSImage imageNamed:@"btnmain_lb.png"], [NSImage imageNamed:@"btnmain_bg.png"], [NSImage imageNamed:@"btnmain_rb.png"],
                                 NO, NSCompositeSourceAtop, 1.0, NO);
            break;
    }

    [super drawRect:dirtyRect];
}

Then I could put the buttons using Interface Builder, and to get custom graphics, I just need to change the class to a new subclass.

+7

:

[self.addBuddyCommitButton.cell setBezelStyle:NSRoundedBezelStyle];
+2

NSButton , UIButton ( , Apple ). NSView . , , , .

+1

All Articles