使用模板图像的 HUD 按钮

发布于 2024-11-13 09:05:43 字数 264 浏览 6 评论 0原文

我正在我的应用程序中构建一个半透明浮动 HUD 窗口,其目的是看起来像“快速查看 HUD”窗口,特别是使用“进入/退出”全屏图像。 Cocoa 提供了 NSEnterFullScreenTemplate (和 NSExitFullScreenTemplate)模板,它可以用于此目的,并且可以在有边框的按钮上正常工作。

但是,一旦我删除边框并将按钮放在深色背景上,它就会保持深灰色,并且很难看到。我想把它变成白色,就像快速查看一样。有没有内置的方法可以做到这一点,或者我是否必须自己缩放和着色图像?

I'm building a semi-transparent floating HUD window in my application, which is intended to look like the Quick Look HUD window, specifically using the Enter/Exit full screen image. Cocoa provides the NSEnterFullScreenTemplate (and NSExitFullScreenTemplate) templates, which serve this purpose, and work fine on bordered buttons.

As soon as I remove the border and put the button on a dark background though, it keeps a dark gray color, and makes it difficult to see. I'd like to make it white, like in Quick Look. Is there a built-in way to do this, or do I have to resort to scaling and coloring the image myself?

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

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

发布评论

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

评论(2

自此以后,行同陌路 2024-11-20 09:05:43

我最终编写了一个 NSImage 类别类方法,该方法返回所需尺寸和所需颜色的模板图像(基本上是我自己做的,因为我不认为 API 确实提供了执行此操作的方法)。

+(NSImage *)templateImage:(NSString *)templateName
                withColor:(NSColor *)tint
                  andSize:(CGSize)targetSize
{
    NSImage *template = [NSImage imageNamed:templateName];
    NSSize size = (CGSizeEqualToSize(targetSize, CGSizeZero)
                   ? [template size]
                   : targetSize);
    NSRect imageBounds = NSMakeRect(0, 0, size.width, size.height);

    NSImage *copiedImage = [template copy];
    [copiedImage setTemplate:NO];
    [copiedImage setSize:size];

    [copiedImage lockFocus];

    [tint set];
    NSRectFillUsingOperation(imageBounds, NSCompositeSourceAtop);

    [copiedImage unlockFocus];

    return [copiedImage autorelease];
}

I ended up writing an NSImage category class method that returns a template image at the desired size and in the desired color (basically doing it myself, as I don't think the API does provide a way to do this).

+(NSImage *)templateImage:(NSString *)templateName
                withColor:(NSColor *)tint
                  andSize:(CGSize)targetSize
{
    NSImage *template = [NSImage imageNamed:templateName];
    NSSize size = (CGSizeEqualToSize(targetSize, CGSizeZero)
                   ? [template size]
                   : targetSize);
    NSRect imageBounds = NSMakeRect(0, 0, size.width, size.height);

    NSImage *copiedImage = [template copy];
    [copiedImage setTemplate:NO];
    [copiedImage setSize:size];

    [copiedImage lockFocus];

    [tint set];
    NSRectFillUsingOperation(imageBounds, NSCompositeSourceAtop);

    [copiedImage unlockFocus];

    return [copiedImage autorelease];
}
失而复得 2024-11-20 09:05:43
[NSCell setBackgroundStyle: NSBackgroundStyleDark]

NSBackgroundStyleDark

背景是深色的。
浅色内容与深色背景形成鲜明对比。

[NSCell setBackgroundStyle: NSBackgroundStyleDark]

NSBackgroundStyleDark

The background is a dark color.
Light contents contrast well with the dark background.

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