9

我正在动态地将 a 添加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];
4

3 回答 3

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

这就是结果

替代文字 http://www.freeimagehosting.net/uploads/26c04b6b64.png

我看不到任何白盒子。
也许您使用的是不同的操作系统。
或者,也许您有一些其他观点相互叠加,导致您正在谈论的奇怪效果。

于 2010-07-09T13:18:26.087 回答
2

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

于 2012-05-20T14:53:26.610 回答
0

好的,

谜团部分解开了。结合我的 NSTextField,我还设置了一些 NSApplicationPresentationOptions 以将应用程序置于 Kiosk 模式。似乎与此有关的东西导致了我所看到的问题。如果我没有设置 PresentationOptions NSTextField 完全按照我想要的方式显示。我将追查具体的 PresentationOption 应该归咎于什么,并在此处发布。

于 2010-07-09T13:46:26.710 回答