我想知道如何在swift中将标题颜色更改为NSButton,我在objective-c中见过很多示例,但我认为在swift中的实现是不同的,有人能给我提供一个示例吗?
发布于 2015-01-11 01:21:56
在viewDidLoad或其他地方尝试一下。
在Swift 3中:
let pstyle = NSMutableParagraphStyle()
pstyle.alignment = .center
button.attributedTitle = NSAttributedString(string: "Title", attributes: [ NSForegroundColorAttributeName : NSColor.red, NSParagraphStyleAttributeName : pstyle ])发布于 2017-12-12 11:17:35
Swift 4
我将此作为附加答案添加,因为它只更改请求的属性,而不覆盖或添加附加属性。
if let mutableAttributedTitle = button.attributedTitle.mutableCopy() as? NSMutableAttributedString {
mutableAttributedTitle.addAttribute(.foregroundColor, value: NSColor.white, range: NSRange(location: 0, length: mutableAttributedTitle.length))
button.attributedTitle = mutableAttributedTitle
}发布于 2017-06-13 10:14:25
在Swift4中
button.attributedTitle = NSMutableAttributedString(string: "Hello World", attributes: [NSAttributedStringKey.foregroundColor: NSColor.white, NSAttributedStringKey.paragraphStyle: style, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 18)])https://stackoverflow.com/questions/27875813
复制相似问题