Как я могу изменить цвет NSButton?
Я использовал метод setBackgroundColor для изменения цвета NSButton, но мой вопрос, есть ли другие методы, которые я мог бы использовать, чтобы изменить цвет NSButton, кроме этого?
2 ответа
Я не знаю, что здесь происходит, но у меня работает следующее:
[[myButton cell] setBackgroundColor:[NSColor redColor]];
Но ТОЛЬКО если я перезаписываю следующий метод в моем подклассе NSButtonCell:
- (void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView
{
[super drawBezelWithFrame:frame inView:controlView];
}
Интересный факт: он даже не называется (проверено с помощью отладчика).
Чтобы изменить цвет или 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;
}