Skip to content

Instantly share code, notes, and snippets.

@GJNilsen
Last active May 14, 2023 15:24
Show Gist options
  • Select an option

  • Save GJNilsen/3d960caf924c25c67c0e967bdcf2c51a to your computer and use it in GitHub Desktop.

Select an option

Save GJNilsen/3d960caf924c25c67c0e967bdcf2c51a to your computer and use it in GitHub Desktop.
Subclass of NSButton
import AppKit
public class FlatButton: NSButton {
public var buttonColor: NSColor = NSColor(calibratedRed: 0.201, green: 0.404, blue: 0.192, alpha: 1)
public var onClickColor: NSColor = NSColor(calibratedRed: 0.304, green: 0.601, blue: 0.294, alpha: 1)
public var textColor: NSColor = NSColor.white
public override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let rectanglePath = NSBezierPath(rect: NSRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height))
var fillColor: NSColor
var strokeColor: NSColor
rectanglePath.fill()
if self.isHighlighted {
strokeColor = self.buttonColor
fillColor = self.onClickColor
} else {
strokeColor = self.onClickColor
fillColor = self.buttonColor
}
strokeColor.setStroke()
rectanglePath.lineWidth = 5
rectanglePath.stroke()
fillColor.setFill()
rectanglePath.fill()
bezelStyle = .shadowlessSquare
let textRect = NSRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
let textTextContent = self.title
let textStyle = NSMutableParagraphStyle()
textStyle.alignment = .center
let textFontAttributes : [ NSAttributedString.Key : Any ] = [
.font: NSFont(name: "SFUIText-Medium", size: NSFont.systemFontSize)!,
.foregroundColor: textColor,
.paragraphStyle: textStyle
]
let textTextHeight: CGFloat = textTextContent.boundingRect(with: NSSize(width: textRect.width, height: CGFloat.infinity), options: .usesLineFragmentOrigin, attributes: textFontAttributes).height
let textTextRect: NSRect = NSRect(x: 0, y: -3 + ((textRect.height - textTextHeight) / 2), width: textRect.width, height: textTextHeight)
NSGraphicsContext.saveGraphicsState()
textTextContent.draw(in: textTextRect.offsetBy(dx: 0, dy: 3), withAttributes: textFontAttributes)
NSGraphicsContext.restoreGraphicsState()
}
}
@bumios
Copy link

bumios commented Oct 21, 2022

It helps me a lot, thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment