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];
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 Mac OS X 10.6.4 上尝试了你的代码。
在应用程序委托内部:
这是结果
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:
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.
尝试设置 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.好吧,
这个谜团已经部分解开了。结合我的 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.