2010-07-09 391 views
9

我正在动态地将NSTextField添加到窗口中,并且出现渲染问题。我将背景色设置为黑色,文本颜色为白色。这两者都可以工作,但是它们看起来像是一个长方形,它是始终为白色的文本的一部分。有谁知道我可能会做错什么?我怎样才能摆脱文本周围的白色背景?代码如下: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]; 

回答

11

我在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]; 
} 

这是结果

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

我看不到任何的白色盒子。
也许你正在使用不同的操作系统。
或者也许你有一些其他的意见在彼此的顶部造成你说的奇怪的影响。

+0

感谢您的快速反应弗洛林,也许是重要的东西,我将NSTextField添加到动态创建的具有黑色背景的NSWindow。不知道这是否有所作为,但它必须考虑到你似乎没有同样的问题,我是。 – 2010-07-09 13:37:03

+0

链接已损坏。你为什么没有把它上传到SO? – SpaceDog 2017-07-15 00:16:10

0

好吧,

这个谜已部分解决。结合我的NSTextField,我还设置了一些NSApplicationPresentationOptions将应用程序置于Kiosk模式。看来有些事情正在导致我所看到的问题。如果我不设置PresentationOptions,则NSTextField将以我想要的方式完全显示。我将追查什么具体的PresentationOption是责备和张贴在这里。

+0

看来,致电 [NSApp setPresentationOptions:] 是责任。即使我将其设置为默认值,也会导致文本具有白色背景。嗯..... – 2010-07-09 13:54:10

2

尝试设置您的NSTextField对象的属性refusesFirstResponder = TRUE。我遇到了你在10.7中描述的行为,在10.6中,一切都按预期工作。

+0

对于我在塞拉利昂没有什么不同 – SpaceDog 2017-07-15 00:12:48