NSButton how to color the text

后端 未结 12 1512
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 00:35

on OSX I have an NSButton with a pretty dark image and unfortunately it is not possible to change the color using the attributes inspector. See picture the big black button,

12条回答
  •  -上瘾入骨i
    2020-12-13 01:16

    Swift 5

    You can use this extension:

    extension NSButton {
    
    @IBInspectable var titleColor: NSColor? {
        get {
            return   NSColor.white
        }
        set {
            let pstyle = NSMutableParagraphStyle()
            pstyle.alignment = .center
            
            self.attributedTitle = NSAttributedString(
                string: self.title,
                attributes: [ NSAttributedString.Key.foregroundColor :newValue, NSAttributedString.Key.paragraphStyle: pstyle])
            
        }
       }
     }
    

    Now you can set title color in storyboard easily. Cheers!

提交回复
热议问题