简体   繁体   中英

how to change dock icon using setContentView to display one big character in mac os x

I want to change the dock icon of an app into one big character like an "A" or "B" for example using swift or objective C

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    @IBOutlet weak var dockView: NSView!
    @IBOutlet weak var dockText: NSTextField!
    let appDockTile =  NSApplication.sharedApplication().dockTile

    func prepareDock(){
        appDockTile.contentView = dockView
        appDockTile.display()
    }
    func changeText(){
        dockText.stringValue = "B"
         appDockTile.display()
    }
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
        prepareDock()

    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }

    @IBAction func btnChangeText(sender: AnyObject) {
        changeText()
    }

}

my two cents for OSX swift 4.x:

(make it flash..)

...

self.HeartBeatTimer = Timer.scheduledTimer(withTimeInterval: DELTA_T, repeats: true, block: { (t: Timer) in

      let name = colored ? "heartbeat" : "heartbeat_red"
      let image = NSImage(named:  name)            
      let appDockTile =  NSApplication.shared.dockTile
      appDockTile.contentView = NSImageView(image: image!)
      appDockTile.display()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM