2015-01-10 14 views
7

Chciałbym wiedzieć, jak zmienić kolor tytułu na NSButton w szybkim, widziałem wiele przykładów w obiektywnym c, ale myślę, że szybkie wdrożenie jest inne, czy każdy może mi dostarczyć przykład?Swift Mac OSX NSButton tytuł koloru

Odpowiedz

19

spróbuj tego w viewDidLoad lub gdzieś.

w Swift 3:

let pstyle = NSMutableParagraphStyle() 
    pstyle.alignment = .center 

    button.attributedTitle = NSAttributedString(string: "Title", attributes: [ NSForegroundColorAttributeName : NSColor.red, NSParagraphStyleAttributeName : pstyle ]) 
+0

Dziękuję człowiekowi, że zrobiłeś mój dzień !!! – PhiceDev

+3

W Swift 2 trzeba zmienić '.CenterTextAlignment' do' .Center' – Sebastian

+3

Swift 3 wersja: 'niech paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center button.attributedTitle = NSAttributedString (wyrażenie: "Tytuł", atrybuty: [NSForegroundColorAttributeName: NSColor.red, NSParagraphStyleAttributeName: paragraphStyle]) ' –

0

W Swift4

button.attributedTitle = NSMutableAttributedString(string: "Hello World", attributes: [NSAttributedStringKey.foregroundColor: NSColor.white, NSAttributedStringKey.paragraphStyle: style, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 18)]) 
+0

Xcode 9 z Swiftem 4 jeszcze nie jest i nie jest GMem, chociaż to się nie zmieni Myślę, że skaczecie tutaj – Chris

1

Swift 4

Dodałem to jako dodatkową odpowiedź, ponieważ zmienia się tylko żądany atrybut bez nadpisywania lub dodawanie kolejnych.

if let mutableAttributedTitle = button.attributedTitle.mutableCopy() as? NSMutableAttributedString { 
    mutableAttributedTitle.addAttribute(.foregroundColor, value: NSColor.white, range: NSRange(location: 0, length: mutableAttributedTitle.length)) 
    button.attributedTitle = mutableAttributedTitle 
}