Change color to a title of a button in Swift [duplicate]

泪湿孤枕 提交于 2020-01-05 08:12:34

问题


I have problems changing the color of the title of a NSButton. I tried this:

button.setTitleColor(NSColor.blackColor())

or this

button.titleColor = NSColor.blackColor()

and this

button.title.textColor = NSColor.blackColor()

but none of these three methods seems to be available! How could that be possible, the compiler says that these method are not present.

AppKit and Cocoa are imported. What is the solution?


回答1:


EDIT

Just realized that the question was for OS X. Anyway, this is how you would do it:

let style = NSMutableParagraphStyle()
    style.alignment = .CenterTextAlignment

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


来源:https://stackoverflow.com/questions/28608727/change-color-to-a-title-of-a-button-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!