Objective c NSTextField颜色问题

Objective c NSTextField颜色问题,objective-c,nstextfield,Objective C,Nstextfield,我正在动态地将NSTextField添加到窗口中,并且在渲染方面遇到问题。我将背景颜色设置为黑色,文本颜色设置为白色。这两种方法都有效,但它们的矩形似乎是文本的一部分,始终为白色。有人知道我可能做错了什么吗?我怎样才能摆脱文本周围的白色背景?代码如下: //Create rectangle to size text field NSRect textFieldRect = NSMakeRect(300, 300, 300, 54); //Instantiate text field and

我正在动态地将
NSTextField
添加到窗口中,并且在渲染方面遇到问题。我将背景颜色设置为黑色,文本颜色设置为白色。这两种方法都有效,但它们的矩形似乎是文本的一部分,始终为白色。有人知道我可能做错了什么吗?我怎样才能摆脱文本周围的白色背景?代码如下:

//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];

我在Mac OS X 10.6.4上试过你的代码

在应用程序委托内部:

- (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];
}
这就是结果

我看不到任何白色的盒子。
可能您正在使用不同的操作系统。
或者,你可能有一些其他的观点相互重叠,导致了你所说的奇怪效果。

好的


谜团已部分解开。结合我的NSTextField,我还设置了一些NSApplicationPresentationOptions,以将应用程序置于Kiosk模式。似乎是某种原因导致了我所看到的问题。如果我没有设置PresentationOptions,NSTextField将以我希望的方式显示。我将跟踪具体的PresentationOption是什么原因,并在此处发布。

尝试设置NSTextField对象的
拒绝FirstResponder=TRUE
属性。我遇到了您在10.7和10.6中描述的行为,一切正常。

感谢您的快速响应Florin,也许是重要的一点,我正在将NSTextField添加到动态创建的黑色背景NSWindow中。不确定这是否有什么不同,但必须考虑到你似乎没有和我一样的问题。链接断开了。为什么您没有将其上传到SO?似乎应该归咎于对[NSApp setPresentationOptions:]的调用。即使我将其设置为默认值,也会导致文本背景为白色。嗯……在塞拉山对我来说没什么区别