Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextField Color issues

I am dynamically adding a NSTextField to a window and I am having issues with rendering. I am setting the background color to be black and the text color to be white. These both work but their is what appears to be a rectangle that is part of the text that is always white. Does anyone know what I might be doing wrong? How can I get rid of the white background that is just around the text? Code is as follows:

//Create rectangle to size text field

NSRect textFieldRect = NSMakeRect(300, 300, 300, 54);

//Instantiate text field and set defaults
NSTextField* textField = [[NSTextField alloc] initWithFrame:textFieldRect];

[textField setFont:[NSFont fontWithName:@"Arial" size:48]];

[textField setTextColor:[NSColor whiteColor]];

[textField setStringValue:@"Some Text"];

[textField setBackgroundColor:[NSColor blackColor]];

[textField setDrawsBackground:YES];

[textField setBordered:NO];

[[window contentView] addSubview:textField];
like image 336
John Ten Cate Avatar asked Jul 09 '10 12:07

John Ten Cate


2 Answers

I tried your code on Mac OS X 10.6.4.

Inside the application delegate:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSRect textFieldRect = NSMakeRect(300, 300, 300, 54);
    NSTextField* textField = [[NSTextField alloc] initWithFrame:textFieldRect];
    [textField setFont:[NSFont fontWithName:@"Arial" size:48]];
    [textField setTextColor:[NSColor whiteColor]];
    [textField setStringValue:@"Some Text"];
    [textField setBackgroundColor:[NSColor blackColor]];
    [textField setDrawsBackground:YES];
    [textField setBordered:NO];
    [[window contentView] addSubview:textField];
}

And this is the result

alt text http://www.freeimagehosting.net/uploads/26c04b6b64.png

I can't see any white box.
Maybe you are using a different OS.
Or maybe you have some other views on top of each other that are causing the weird effect you are talking about.

like image 143
Florin Avatar answered Sep 16 '22 15:09

Florin


Try setting refusesFirstResponder = TRUE property of your NSTextField object. I have come across behavior you described in 10.7, in 10.6 everything works as expected.

like image 28
Rognarroc Avatar answered Sep 20 '22 15:09

Rognarroc