如何在不为其设置 keyEquivalent 的情况下为 NSButton 赋予蓝色样式?

问题描述 投票:0回答:1

我将下面的代码用于我的 NSButton,以获得蓝色样式的 Button。但是这段代码让我的 Button 对键盘上的返回键做出反应,这不是我想要的,我的目标是拥有这种风格而不必为我的 Button 设置 keyEquivalent,我如何为这个目标定制我的 NSButton?

let myButton: NSButton = NSButton()
myButton.bezelStyle = .rounded
myButton.keyEquivalent = "\r"
swift cocoa nsbutton
1个回答
0
投票

以下源代码将使用内联图像创建蓝色 NSButton,该图像是与按钮大小接近的蓝色圆角矩形。按钮标题颜色更改为白色以使其更加明显。

// **** 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)

© www.soinside.com 2019 - 2024. All rights reserved.