如何实现HUD风格的窗口,如地址簿的“以大字体显示”
包括内置通讯簿在内的多个应用程序使用半透明的 HUD 窗口,并带有大的阴影文本。我想在我的 Cocoa Mac 应用程序中实现类似的窗口。
是否有这种窗口的免费实现?
如果没有,实施它的最佳方法是什么?
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.
Is there a free implementation of this kind of window somewhere?
If not, what is the best way to implement it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个示例项目,展示了如何执行此操作:
http://github.com/NSGod/ BlackBorderlessWindow
基本上,您需要创建一个无边框的
NSWindow
子类。最简单的方法是在 nib 文件中设置窗口大小和排列,然后将其类设置为自定义子类。因此,虽然它在 Interface Builder 中看起来仍然像普通窗口,但在运行时它会按照您的需要显示。alpha 值将使窗口半透明。
此外,您还可以创建一个自定义
NSView
子类来绘制圆角矩形:与窗口一样,您只需将窗口的 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.The alpha value will make the window semi-transparent.
Also, you can create a custom
NSView
subclass that will draw a round rectangle: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 nestedNSView
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.
感谢您分享此代码。对我帮助很大!
您可以将以下行...添加
到窗口的 init 函数中。这将删除白色的角。
Thank you for sharing this code. Helped me a lot!
You may add the following line...
to the init function of the window. This removes the white corners.