I created a custom NSButtonCell subclass which allows customizing padding between a button's contents. In my implementation (the full source code can be found on GitHub) I override titleRect(forBounds:) to position the button title:
var titleSize: NSSize {
return NSSize(width: ceil(attributedTitle.size().width),
height: ceil(attributedTitle.size().height))
}
override func titleRect(forBounds rect: NSRect) -> NSRect {
return CGRect(x: paddingLeft,
y: rect.height / 2 - titleSize.height / 2,
width: titleSize.width,
height: titleSize.height)
}
The result doesn't look good:
To get the desired outcome I have to add an extra padding to the width:

I also tried using boundingRect(with:options:context:) to get the size, but I got the same results.

For future reference: I figured out the issue. When using
attributedTitle, it's important to specify the font of the button, so thatattributedString.size()can calculate the necessary width correctly. I assumed that by default, calculations are based on the default font forNSButtonbut apparently that was incorrect. See my commit for more details.