我想让我的NSButton看起来像Xcode的提交按钮
但似乎没有任何方法可以轻松更改NSButton的背景色。您必须将“边框”更改为“否”,然后设置按钮的背景色,并为按钮设置“attributedTitle”,使文本颜色变为白色。但当我这么做的时候,结果看起来一点也不接近:
按钮没有任何阴影;文本看起来不居中;它也不支持在选择按钮时更改背景颜色,就像Xcode中的按钮一样。
这肯定很容易复制,因为我相信我在整个系统中都见过类似的按钮。
下面是我为此编写的NSButton子类:
override func awakeFromNib() {
super.awakeFromNib()
self.wantsLayer = true
self.isBordered = false //Important
self.layer?.backgroundColor = backgroundColor.cgColor
self.layer?.cornerRadius = 6.0
let font = NSFont.systemFont(ofSize: 14, weight: .medium)
let fontColor = NSColor.white
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
self.attributedTitle = NSAttributedString(string: self.title, attributes: [NSAttributedString.Key.foregroundColor : fontColor,
NSAttributedString.Key.font: font, NSAttributedString.Key.paragraphStyle : paragraphStyle]) // to update text, have to update the attributeString's mutableString property
}
我很想知道我的错误在哪里。我本以为我需要一个带有边框和阴影效果的常规“按钮”,但背景是蓝色的,但从我所看到的情况来看,这似乎并不太好。
谢谢