Fingindo um NSTextField usando um NSTextView para obter uma boa coloração?

Tentando alterar a cor do plano de fundo do texto selecionado para um NSTextField (temos uma interface de usuário escura e o plano de fundo do texto selecionado é quase o mesmo que o próprio texto), mas apenas o NSTextView parece nos permitir alterar isso.

Portanto, estamos tentando falsificar um NSTextField usando um NSTextView, mas não podemos fazer com que a rolagem de texto funcione da mesma maneira.

O mais próximo que chegamos é desse código:

NSTextView *tf = [ [ NSTextView alloc ] initWithFrame: NSMakeRect( 30.0, 20.0, 80.0, 22.0 ) ];

// Dark UI
[tf setTextColor:[NSColor whiteColor]];
[tf setBackgroundColor:[NSColor darkGrayColor]];

// Fixed size
[tf setVerticallyResizable:FALSE];
[tf setHorizontallyResizable:FALSE];

[tf setAlignment:NSRightTextAlignment]; // Make it right-aligned (yup, we need this too)

[[tf textContainer] setContainerSize:NSMakeSize(2000, 20)]; // Try to Avoid line wrapping with this ugly hack
[tf setFieldEditor:TRUE]; // Make Return key accept the textfield

// Set text properties
NSMutableDictionary *dict = [[[tf selectedTextAttributes] mutableCopy ] autorelease];
[dict setObject:[NSColor orangeColor] forKey:NSBackgroundColorAttributeName];
[tf setSelectedTextAttributes:dict];

Isso funcionaquase tudo bem, exceto que se o texto for maior que o campo de texto, você não poderá rolar para ele de forma alguma.

Alguma idéia de como fazer isso?

desde já, obrigado

Edit: Solução sugerida abaixo porJoshua Nozzi

Graças a Joshua, esta é uma ótima solução para o que eu estava procurando:

@interface ColoredTextField : NSTextField
- (BOOL)becomeFirstResponder;
@end

@implementation ColoredTextField
- (BOOL)becomeFirstResponder
{
    if (![super becomeFirstResponder])
        return NO;

    NSDictionary * attributes = [NSDictionary dictionaryWithObjectsAndKeys : 
                     [NSColor orangeColor], NSBackgroundColorAttributeName, nil];

    NSTextView * fieldEditor = (NSTextView *)[[self window] fieldEditor:YES forObject:self];
    [fieldEditor setSelectedTextAttributes:attributes];
    return YES;
}
@end

Em vez de falsificá-lo com um NSTextView, é apenas um NSTextField que altera a cor do texto selecionado quando ele se torna o primeiro a responder.

Editar: O código acima retorna à cor de seleção padrão quando você pressiona Enter no campo de texto. Aqui está uma maneira de evitar isso.

@interface ColoredTextField : NSTextField
- (BOOL)becomeFirstResponder;
- (void)textDidEndEditing:(NSNotification *)notification;

- (void)setSelectedColor;
@end

@implementation ColoredTextField
- (BOOL)becomeFirstResponder
{
    if (![super becomeFirstResponder])
        return NO;
    [self setSelectedColor];
    return YES;
}

- (void)textDidEndEditing:(NSNotification *)notification
{
    [super textDidEndEditing:notification];
    [self setSelectedColor];
}

- (void) setSelectedColor
{
    NSDictionary * attributes = [NSDictionary dictionaryWithObjectsAndKeys : 
                                [NSColor orangeColor], NSBackgroundColorAttributeName, nil];

    NSTextView * fieldEditor = (NSTextView *)[[self window] fieldEditor:YES forObject:self];
    [fieldEditor setSelectedTextAttributes:attributes];
}
@end

questionAnswers(1)

yourAnswerToTheQuestion