如何实现HUD风格的窗口,如地址簿的“以大字体显示”

发布于 2024-10-07 15:27:27 字数 194 浏览 7 评论 0 原文

包括内置通讯簿在内的多个应用程序使用半透明的 HUD 窗口,并带有大的阴影文本。我想在我的 Cocoa Mac 应用程序中实现类似的窗口。

screenshot

是否有这种窗口的免费实现?

如果没有,实施它的最佳方法是什么?

Several apps, including the built-in Address Book use a HUD window that is semi-transparent, with large shadowed text. I'd like to implement a similar window in my Cocoa Mac app.

screenshot

Is there a free implementation of this kind of window somewhere?

If not, what is the best way to implement it?

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

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

发布评论

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

评论(2

哥,最终变帅啦 2024-10-14 15:27:27

这是一个示例项目,展示了如何执行此操作:

http://github.com/NSGod/ BlackBorderlessWindow

基本上,您需要创建一个无边框的 NSWindow 子类。最简单的方法是在 nib 文件中设置窗口大小和排列,然后将其类设置为自定义子类。因此,虽然它在 Interface Builder 中看起来仍然像普通窗口,但在运行时它会按照您的需要显示。

@implementation MDBorderlessWindow

- (id)initWithContentRect:(NSRect)contentRect
            styleMask:(NSUInteger)windowStyle
              backing:(NSBackingStoreType)bufferingType
                defer:(BOOL)deferCreation {

    if (self = [super initWithContentRect:contentRect
                            styleMask:NSBorderlessWindowMask
                              backing:NSBackingStoreBuffered defer:deferCreation]) {
            [self setAlphaValue:0.75];
            [self setOpaque:NO];
            [self setExcludedFromWindowsMenu:NO];
    }
    return self;
}

alpha 值将使窗口半透明。

此外,您还可以创建一个自定义 NSView 子类来绘制圆角矩形:

@implementation MDBlackTransparentView

- (id)initWithFrame:(NSRect)frame {
    if (self = [super initWithFrame:frame]) {

    }
    return self;
}

- (void)drawRect:(NSRect)frame {
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:frame
                                    xRadius:6.0 yRadius:6.0];
    [[NSColor blackColor] set];
    [path fill];
}

@end

与窗口一样,您只需将窗口的 contentView 类设置为您的自定义 NSView 子类。 (使用大纲视图模式并单击显示三角形以在 nib 文件中的窗口图标内显示嵌套的 NSView)。同样,虽然视图在 Interface Builder 中看起来很普通,但在运行时看起来还不错。

然后只需在视图顶部放置一个 NSTextField 并相应地设置文本即可。

请注意,一般来说,无边框窗口不容易使用(例如,如果您希望能够拖动窗口,则需要自己添加该功能)。例如,Apple 有一些关于如何允许拖动的示例代码。

替代文本

Here's a sample project that shows how to do it:

http://github.com/NSGod/BlackBorderlessWindow

Basically, you need to create a borderless NSWindow subclass. The easiest way to do this is to set your window size and arrangement in the nib file, and then set its class to be your custom subclass. So while it will still look like a normal window in Interface Builder, at runtime it will appear as you need it to.

@implementation MDBorderlessWindow

- (id)initWithContentRect:(NSRect)contentRect
            styleMask:(NSUInteger)windowStyle
              backing:(NSBackingStoreType)bufferingType
                defer:(BOOL)deferCreation {

    if (self = [super initWithContentRect:contentRect
                            styleMask:NSBorderlessWindowMask
                              backing:NSBackingStoreBuffered defer:deferCreation]) {
            [self setAlphaValue:0.75];
            [self setOpaque:NO];
            [self setExcludedFromWindowsMenu:NO];
    }
    return self;
}

The alpha value will make the window semi-transparent.

Also, you can create a custom NSView subclass that will draw a round rectangle:

@implementation MDBlackTransparentView

- (id)initWithFrame:(NSRect)frame {
    if (self = [super initWithFrame:frame]) {

    }
    return self;
}

- (void)drawRect:(NSRect)frame {
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:frame
                                    xRadius:6.0 yRadius:6.0];
    [[NSColor blackColor] set];
    [path fill];
}

@end

Like with the window, you simply set the class of the window's contentView to be your custom NSView subclass. (Use outline view mode and click the disclosure triangle to show the nested NSView inside the icon of the window in the nib file). Again, while the view will look ordinary in Interface Builder, it will look okay at runtime.

Then just place an NSTextField on top of view and set the text accordingly.

Note that, in general, borderless windows aren't easy to work with (for example, if you want to be able to drag the window around, you'll need to add that functionality back yourself). Apple has some sample code on how to allow dragging, for instance.

alt text

乖乖公主 2024-10-14 15:27:27

感谢您分享此代码。对我帮助很大!
您可以将以下行...添加

[self setBackgroundColor:[NSColor clearColor]];

到窗口的 init 函数中。这将删除白色的角。

Thank you for sharing this code. Helped me a lot!
You may add the following line...

[self setBackgroundColor:[NSColor clearColor]];

to the init function of the window. This removes the white corners.

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