Свифт 4.
Предыдущий ответ помог мне. Спасибо.
extension NSButton {
public func setText(text: String, color: NSColor, font: NSFont) {
let paragraphStyle = NSMutableParagraphStyle.init()
paragraphStyle.alignment = .center
let range = NSRange.init(location: 0, length: (text.lengthOfBytes(using: String.Encoding.utf8)))
let attributedTitle = NSMutableAttributedString.init(string: text)
attributedTitle.addAttribute( NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: range)
attributedTitle.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
attributedTitle.addAttribute(NSAttributedString.Key.font, value: font, range: range)
self.attributedTitle = attributedTitle
}
public func setBackground(color: NSColor, radius: CGFloat = 2.0) {
self.wantsLayer = true
self.isBordered = false
self.layer?.backgroundColor = color.cgColor
self.layer?.cornerRadius = radius
}
}
Как использовать для настройки NSButton с помощью IBOutlet:
refreshButton.setText(text: "REFRESH", color: .white, font: .systemFont(ofSize: 13))
refreshButton.setBackground(color: .red, radius: 2.0)