Trying to change the selected text background color for an NSTextField (we have a dark UI, and selected text background is almost the same as the text itself), but only NSTextView seems to allow us to change this.
So we're trying to fake an NSTextField using an NSTextView, but can't get text scrolling to work the same.
The closest we get is with this code:
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];
This works almost alright, except that if the text is longer than the text field, you can't scroll to it in any way.
Any idea as how to accomplish this?
Thanks in advance
Edit: Solution suggested below by Joshua Nozzi
Thanks to Joshua, this is a great solution to what I was looking for:
@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
Instead of faking it with an NSTextView, it's just an NSTextField that changes the selected text color when it becomes first responder.
Edit: The above code falls back to the default selection color once you press Enter in the textfield. Here's a way to avoid that.
@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