How to give a blue style to a NSButton without having to set a keyEquivalent for it?

83 Views Asked by At

I am using this code below for my NSButton, for having the blue style Button. But this code make my Button react to return key on keyboard, which it is not my wish, my goal is having this style without having to set a keyEquivalent for my Button, how can I customize my NSButton for this goal?

let myButton: NSButton = NSButton()
myButton.bezelStyle = .rounded
myButton.keyEquivalent = "\r"
1

There are 1 best solutions below

0
apodidae On

The following source code will create a blue NSButton using an inline image which is a blue colored rounded rect near the same size as the button. The button title color was changed to white to make it more visible.

// **** Inline Image **** //
let fillColor = NSColor(calibratedRed: 0.0, green: 0.0, blue: 1.0, alpha: 1.0)
let image = NSImage.init(size: NSMakeSize(128.0, 22.0))
let imgRect = NSMakeRect(0.0, 0.0, 128.0, 22.0)
image.lockFocus()
let roundedRect = NSBezierPath (roundedRect:imgRect, xRadius:4, yRadius:4)
fillColor.set()
roundedRect.fill()
image.unlockFocus()

// **** Button **** //
let myBtn = NSButton (frame:NSMakeRect( 150, 100, 140, 24 ))
myBtn.bezelStyle = .rounded
myBtn.image = image
let attributes: [NSAttributedString.Key: Any] = [.font: NSFont.systemFont(ofSize: 12), .foregroundColor: NSColor.white]
myBtn.attributedTitle = NSMutableAttributedString(string: "Show Window", attributes:attributes)
window.contentView!.addSubview (myBtn)