我正在尝试使用状态栏项创建一个macOS应用,该状态栏项使用PDF而不是PNG图像,但是当我使用该图像时,它的尺寸过大,整个状态项为黑色。我找不到缩放图像的方法,以使其适合其他Mac状态栏项目。
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
if let button = statusItem.button {
button.image = NSImage(named:NSImage.Name("status-logo"))
button.action = #selector(printQuote(_:))
}
}
@objc func printQuote(_ sender: Any?) {
let quoteText = "Never put off until tomorrow what you can do the day after tomorrow."
let quoteAuthor = "Mark Twain"
print("\(quoteText) — \(quoteAuthor)")
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
您应该调整pdf的大小,以使其适合statusItem
的按钮,如下所示:
guard let logo = NSImage(named: NSImage.Name("status-logo")) else { return }
let resizedLogo = NSImage(size: NSSize(width: 18, height: 18), flipped: false) { (dstRect) -> Bool in
logo.draw(in: dstRect)
return true
}
然后设置button.image = resizedLogo