【问题标题】:Scaling a PDF image for NSStatusItem image为 NSStatusItem 图像缩放 PDF 图像
【发布时间】:2020-02-25 10:56:21
【问题描述】:

我正在尝试创建一个带有状态栏项目的 macOS 应用程序,该项目使用 PDF 而不是 PNG 图像,但是当我使用该图像时,它被过度缩放并且整个状态项目都是黑色的。我找不到缩放图像以使其适合其余 mac 状态栏项目的方法。

图标

  • Font Awesome
    • (我无法附上 pdf,因此您需要手动转换)

资产.xcassets

AppDelegate.swift

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
    }
}

期待

现实

【问题讨论】:

    标签: swift macos statusbar appkit


    【解决方案1】:

    您应该调整 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

    【讨论】:

      猜你喜欢
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2010-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多