I had the same problem, and with bated breath I tried to find solutions in this post. (Although I appreciate the advice that button titles should usually be short, I am writing a game and want multi-line responses to behave like buttons).
Sometimes you don’t get from here. My ideal was an NSButton with a multi-line shortcut, but since I can't get it without much trouble, I created PseudoButton: a subclass of NSControl that behaves like a button. It has a hand cursor indicating “you can click here,” and it gives feedback: when you click the mouse, it changes to selectedControlColor, when you release the mouse, it returns to normal. And unlike solutions that use stacks of buttons and labels, there are no problems placing labels and images at the top of the view: the entire view is an interactive area.
import Cocoa @IBDesignable class PseudoButton: NSControl { @IBInspectable var backgroundColor: NSColor = NSColor.white{ didSet{ self.needsDisplay = true } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) let path = NSBezierPath(rect: dirtyRect) backgroundColor.setFill() path.fill() NSColor.black.setStroke() path.lineWidth = 2 path.stroke() } override func mouseDown(with event: NSEvent) { self.backgroundColor = NSColor.selectedControlColor } override func mouseUp(with event: NSEvent) { self.backgroundColor = NSColor.clear guard let action = action else {return} tryToPerform(action, with: self) //@IBAction func pseudobuttonClicked(_ sender: PseudoButton) in the ViewController class } override func resetCursorRects() { addCursorRect(bounds, cursor: .pointingHand) } }
You use this, like any other control in a storyboard: drag the pseudo-button inward, decorate it as you wish, and connect it to the corresponding IBAction in your viewController class.
I like it better than interfering with NSCell. (According to past experience, NSCell-based hacks break more often).
green_knight
source share