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];
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With