【问题标题】:Change color to a title of a button in Swift [duplicate]将颜色更改为 Swift 中按钮的标题 [重复]
【发布时间】:2015-02-19 14:20:07
【问题描述】:

我在更改 NSButton 的标题颜色时遇到问题。我试过这个:

button.setTitleColor(NSColor.blackColor())

或者这个

button.titleColor = NSColor.blackColor()

还有这个

button.title.textColor = NSColor.blackColor()

但这三种方法似乎都不可用!这怎么可能呢,编译器说这些方法不存在。

AppKit 和 Cocoa 已导入。解决办法是什么?

【问题讨论】:

    标签: xcode macos swift xcode6 nsbutton


    【解决方案1】:

    编辑

    刚刚意识到这个问题是针对 OS X 的。无论如何,这就是你要做的:

    let style = NSMutableParagraphStyle()
        style.alignment = .CenterTextAlignment
    
    button.attributedTitle = NSAttributedString(string: "foo", attributes: [ NSForegroundColorAttributeName : NSColor.blackColor(), NSParagraphStyleAttributeName : style ])
    

    【讨论】:

      猜你喜欢
      • 2014-08-01
      • 2015-12-07
      • 2020-07-14
      • 2015-10-01
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多