2

我一直在使用 setBackgroundColor 方法来更改 NSButton 颜色,但我的问题是,除此之外还有其他方法可以用来更改 NSButton 的颜色吗?

4

2 回答 2

1

要更改颜色或NSButton您需要访问按钮,IBOutlet除非按钮位于 TableView / CollectionView 等中,否则大多数情况下您都会这样做。

说按钮插座是myButton,那么你需要

[[myButton cell] setBackgroundColor:[NSColor redColor]]; //Change the color according to your need

编辑:

您也可以通过NSButtonCell使用这些方法进行子类化来做到这一点

- (void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView
{
    NSGraphicsContext* ctx = [NSGraphicsContext currentContext];

    // corner radius
    CGFloat roundedRadius = 17.0f;

    NSColor *color = [NSColor redColor];

    // Draw darker overlay if button is pressed
    if([self isHighlighted]) {
        [ctx saveGraphicsState];
        [[NSBezierPath bezierPathWithRoundedRect:frame
                                         xRadius:roundedRadius
                                         yRadius:roundedRadius] setClip];
        [[color darkenColorByValue:0.12f] setFill];
        NSRectFillUsingOperation(frame, NSCompositeSourceOver);
        [ctx restoreGraphicsState];

        return;
    }

    // create background color
    [ctx saveGraphicsState];
    [[NSBezierPath bezierPathWithRoundedRect:frame
                                     xRadius:roundedRadius
                                     yRadius:roundedRadius] setClip];
    [[color darkenColorByValue:0.12f] setFill];
    NSRectFillUsingOperation(frame, NSCompositeSourceOver);
    [ctx restoreGraphicsState];


    //draw inner button area
    [ctx saveGraphicsState];

    NSBezierPath* bgPath = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 1.0f, 1.0f) xRadius:roundedRadius yRadius:roundedRadius];
    [bgPath setClip];

    NSColor* topColor = [color lightenColorByValue:0.12f];

    // gradient for inner portion of button
    NSGradient* bgGradient = [[NSGradient alloc] initWithColorsAndLocations:
                              topColor, 0.0f,
                              color, 1.0f,
                              nil];
    [bgGradient drawInRect:[bgPath bounds] angle:90.0f];

    [ctx restoreGraphicsState];
}

- (NSRect) drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView {
    NSGraphicsContext* ctx = [NSGraphicsContext currentContext];

    [ctx saveGraphicsState];
    NSMutableAttributedString *attrString = [title mutableCopy];
    [attrString beginEditing];

    NSColor *titleColor;
    if ([[self getColorForButtonType] isLightColor]) {
        titleColor = [NSColor blackColor];
    } else {
        titleColor = [NSColor whiteColor];
    }

    [attrString addAttribute:NSForegroundColorAttributeName value:titleColor range:NSMakeRange(0, [[self title] length])];
    [attrString endEditing];
    NSRect r = [super drawTitle:attrString withFrame:frame inView:controlView];

    [ctx restoreGraphicsState];

    return r;
}
于 2013-05-06T07:44:45.117 回答
1

我不知道这里发生了什么,但以下对我有用:

[[myButton cell] setBackgroundColor:[NSColor redColor]];

但只有当我在我的 NSButtonCell 子类中覆盖以下方法时:

- (void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView
{
    [super drawBezelWithFrame:frame inView:controlView];
}

有趣的事实:它甚至没有被调用(用调试器测试)。

于 2015-05-07T11:18:25.170 回答