NSTextField 颜色问题

发布于 2024-09-08 15:43:28 字数 700 浏览 6 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你再特殊 2024-09-15 15:43:28

我在 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< /a>

我看不到任何白框。
也许您正在使用不同的操作系统。
或者也许你还有一些其他的观点相互叠加,导致了你所说的奇怪的效果。

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.

吻安 2024-09-15 15:43:28

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

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.

慵挽 2024-09-15 15:43:28

好吧,

这个谜团已经部分解开了。结合我的 NSTextField,我还设置了一些 NSApplicationPresentationOptions 以将应用程序置于 Kiosk 模式。看来是某些东西导致了我所看到的问题。如果我没有设置PresentationOptions,NSTextField 将完全按照我想要的方式显示。我将找出具体的PresentationOption 的罪魁祸首并发布在这里。

Ok,

The mystery is partially solved. In conjunction with my NSTextField, I am also setting some NSApplicationPresentationOptions to put the application into Kiosk mode. It appears that something with that is causing the problem I am seeing. If I do not set the PresentationOptions the NSTextField displays exactly the way I want it to. I will track down what specific PresentationOption is to blame and post here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文