如何在 NSView 子类中绘制两行文本? (macOS 系统托盘)
How to draw two lines of text in a NSView subclass ? (macOS system tray)
我正在尝试在 macOS 系统托盘中写入两行文本,非常像 iStat pro 为网络所做的那样 activity:
我知道我必须将 NSView 子类化才能这样做,但我什至无法让它工作一行:
class CustomView:NSView {
override func draw(_ dirtyRect: NSRect) {
let text: NSString = "my string"
let textRect: NSRect = NSMakeRect(0, 0, 100, 20)
text.draw(in: textRect)
}
}
然后:
let statusBar = NSStatusBar.system
statusItem = statusBar.statusItem(withLength: 100)
let view = CustomView()
view.wantsLayer = true
statusItem.button?.addSubview(view)
我在 Whosebug 上发现了其他线程:
- OS X Status Bar Application with a title in two lines : 它在 objective C 中,我也无法让它工作
- How to show two row of text in a menu bar app in mac os : 不幸的是,解释对我来说不够详细
怀疑这是否是您要查找的内容,但这将在状态项中放置两行文本:
import Cocoa
let app = NSApplication.shared
let statusItem = NSStatusBar.system.statusItem(withLength:-1)
statusItem.button?.font = NSFont(name:"Menlo Bold", size: 10.0)
statusItem.button!.title = "one\ntwo"
app.run()
它仍然只是一个按钮,我不知道您将如何分开选择每个项目。也许下拉菜单会更好?
我正在尝试在 macOS 系统托盘中写入两行文本,非常像 iStat pro 为网络所做的那样 activity:
我知道我必须将 NSView 子类化才能这样做,但我什至无法让它工作一行:
class CustomView:NSView {
override func draw(_ dirtyRect: NSRect) {
let text: NSString = "my string"
let textRect: NSRect = NSMakeRect(0, 0, 100, 20)
text.draw(in: textRect)
}
}
然后:
let statusBar = NSStatusBar.system
statusItem = statusBar.statusItem(withLength: 100)
let view = CustomView()
view.wantsLayer = true
statusItem.button?.addSubview(view)
我在 Whosebug 上发现了其他线程:
- OS X Status Bar Application with a title in two lines : 它在 objective C 中,我也无法让它工作
- How to show two row of text in a menu bar app in mac os : 不幸的是,解释对我来说不够详细
怀疑这是否是您要查找的内容,但这将在状态项中放置两行文本:
import Cocoa
let app = NSApplication.shared
let statusItem = NSStatusBar.system.statusItem(withLength:-1)
statusItem.button?.font = NSFont(name:"Menlo Bold", size: 10.0)
statusItem.button!.title = "one\ntwo"
app.run()
它仍然只是一个按钮,我不知道您将如何分开选择每个项目。也许下拉菜单会更好?