我创建了一个自定义NSButtonCell
子类,它允许自定义按钮内容之间的填充。在我的实现中(完整的源代码可以在GitHub上找到)我重写titleRect(forBounds:)来定位按钮标题:
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)
}
结果看起来不太好:
我也尝试使用boundingRect(with:options:context:)来获取大小,但我得到了相同的结果。