[英]Swift Mac OSX NSButton title color
我想知道如何在 swift 中將標題顏色更改為 NSButton,我在 objective-c 中看到了很多示例,但我認為在 swift 中,任何人都可以提供不同的示例嗎?
在viewDidLoad
或其他地方嘗試一下。
在Swift 3中:
let pstyle = NSMutableParagraphStyle()
pstyle.alignment = .center
button.attributedTitle = NSAttributedString(string: "Title", attributes: [ NSForegroundColorAttributeName : NSColor.red, NSParagraphStyleAttributeName : pstyle ])
斯威夫特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
}
在Swift4中
button.attributedTitle = NSMutableAttributedString(string: "Hello World", attributes: [NSAttributedStringKey.foregroundColor: NSColor.white, NSAttributedStringKey.paragraphStyle: style, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 18)])
Swift 5
button.contentTintColor = NSColor.white
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.