【问题标题】:NSTextField Color issuesNSTextField 颜色问题
【发布时间】:2010-07-09 12:33:45
【问题描述】:

我正在向窗口动态添加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];

【问题讨论】:

    标签: objective-c nstextfield


    【解决方案1】:

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

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

    【讨论】:

    • 感谢 Florin 的快速响应,也许有些重要的事情,我正在将 NSTextField 添加到一个动态创建的具有黑色背景的 NSWindow 中。不确定这是否会有所不同,但必须考虑到您似乎没有遇到与我相同的问题。
    • 链接已损坏。为什么没有上传到 SO?
    【解决方案2】:

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

    【讨论】:

    • 在 Sierra 上对我没有任何影响
    【解决方案3】:

    好的,

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

    【讨论】:

    • 看来应该归咎于对 [NSApp setPresentationOptions: ] 的调用。即使我将其设置为默认值,它也会导致文本具有白色背景。嗯.....
    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 2011-03-22
    • 2011-06-22
    • 2015-03-14
    相关资源
    最近更新 更多