2015-01-10 7 views

答えて

19

viewDidLoadどこかでこれを試してみてください。スウィフト3では

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

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

あなたは私の一日行わおかげで男! 'てみましょうparagraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center button.attributedTitle = NSAttributedString(文字列:あなたは' .Center' – PhiceDev

+3

"タイトル"、属性:[NSForegroundColorAttributeName:NSColor.red、NSParagraphStyleAttributeName:paragraphStyle]) ' – Sebastian

+3

スウィフト3バージョンに '.CenterTextAlignment'を変更する必要がありスウィフト2では –

0

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

Xcodeの9はまだ出ていないと、これは私はあなたがここに銃をジャンプしていると思う変更されないことがありますが、それほどのいずれかGMではありません – Chris

1

スウィフトSwift4で、それは上書きせずにのみ要求された属性を変更しますので、私は追加の答えとしてこれを追加しました

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 
} 
関連する問題