How to style NSButton in AppKit

Issue #297

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
let button = NSButton()
button.wantsLayer = true
button.isBordered = false
button.setButtonType(.momentaryChange)
button.attributedTitle = NSAttributedString(
string: "Click me",
attributes: [
NSAttributedString.Key.foregroundColor: NSColor.white,
NSAttributedString.Key.font: NSFont.labelFont(ofSize: 13)
]
button.layer?.backgroundColor = NSColor.orange.cgColor
button.layer?.cornerRadius = 12

activate(
button.anchor.height.equal.to(32),
button.anchor.width.equal.to(100)
)

To make it have native rounded rect

1
2
3
4
button.imageScaling = .scaleProportionallyDown
button.setButtonType(.momentaryPushIn)
button.bezelStyle = .rounded
button.isBordered = true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import AppKit
import Omnia

extension NSButton {
func style(imageName: String) {
image = NSImage(named: NSImage.Name(imageName))
isBordered = false
imageScaling = .scaleProportionallyDown
}

func styleAction(title: String) {
attributedTitle = NSAttributedString(string: title, attributes: [
NSAttributedString.Key.foregroundColor: NSColor(hex: "008B80"),
NSAttributedString.Key.font: NSFont.boldSystemFont(ofSize: 13)
])

isBordered = false
setButtonType(.momentaryChange)
}
}

Read more

Comments