Pregunta

Por alguna razón, cuando mi botón está deshabilitado, el color de texto se vuelve blanco. Quiero que permanezca negro, ¿cómo puedo hacerlo?

¿Fue útil?

Solución

Puede establecer texto, imagen, colores, fuentes, etc. para un estado diferente de un botón: normal, resaltado, deshabilitado, etc.

Puede hacerlo en Interface Builder cambiando el estado con la lista desplegable.

Otros consejos

Puede subclase nsbuttoncell y anular un método:

- (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView
{
    if (![self isEnabled]) {
        return [super drawTitle:[self attributedTitle] withFrame:frame inView:controlView];
    }

    return [super drawTitle:title withFrame:frame inView:controlView];
}

De esta manera, cuando el botón está deshabilitado, el texto tendrá el mismo color de texto cuando el botón esté habilitado.

También mira esto

[btnInfo.cell setImageDimsWhenDisabled:NO];

Puede anular un método privado en nsbuttoncell:

- (BOOL)_textDimsWhenDisabled {
    return NO;
}

- (BOOL)_shouldDrawTextWithDisabledAppearance {
    return NO;
}

Llené un radar para un método público: rdar: // 19218619

En Mojave, cualquier método de anulación de dibujo hace que sea imposible cambiar el fondo de fondo del nsbutton cuando se resalta. Así que preferiría recomendar usar

- (BOOL)_shouldDrawTextWithDisabledAppearance

para este propósito. Si está usando Swift 4, haría lo siguiente en el encabezado de puente:

#import <AppKit/AppKit.h>
@interface NSButtonCell (Private)
- (BOOL)_shouldDrawTextWithDisabledAppearance;
@end

Y en la subclase de Nsbuttoncell:

override func _shouldDrawTextWithDisabledAppearance() -> Bool {
    return false
}

Actualización para Swift 4:

  override func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) -> NSRect {

    if !self.isEnabled {
        return super.drawTitle(self.attributedTitle, withFrame: frame, in: controlView)
    }

    return super.drawTitle(title, withFrame: frame, in: controlView)
    }

Esto hará que los atributos de texto sean los mismos que cuando el botón está habilitado.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top