2015-04-03 1 views
3

Il existe plusieurs questions sur la façon de définir la couleur du texte par programme. Tout va bien, mais il doit y avoir un moyen de le faire via Interface Builder aussi.Comment puis-je définir la couleur du texte d'un NSButton via Interface Builder?

la case « Afficher les polices de caractères » fonctionne pour changer la taille du texte du bouton, mais Xcode ne tient pas compte des changements de couleur en utilisant le widget là, et les attributs de l'inspecteur pour NSButton ne dispose pas d'un sélecteur de couleur ...

Répondre

-3

Modifier: Question mal comprise. Voici comment modifier le texte d'un bouton sur une application iOS.

Juste pour clarifier, cela ne fonctionne pas pour vous?

  • Ajout d'un bouton
  • cliquez dessus et allez à Attributs inspecteur
  • changement de couleur dans "Couleur du texte" champ

Changed button color to reddish

+1

La question concerne un NSButton (pour une application OS X, pas un iPhone). Il n'y a pas de champ "Couleur du texte" dans l'inspecteur d'attributs (du moins pas à partir de Xcode 6.1.1). – Troy

+0

Woops, mal lu la question - bonne chance – twelveandoh

2

Essayez cette solution, je l'espère vous sera obtenir :)

NSFont *txtFont = button.font; 
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; 
[style setAlignment:button.alignment]; 
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
            [NSColor whiteColor], NSForegroundColorAttributeName, style, NSParagraphStyleAttributeName, txtFont, NSFontAttributeName, nil]; 
NSAttributedString *attrString = [[NSAttributedString alloc] 
             initWithString:button.title attributes:attrsDictionary]; 
[button setAttributedTitle:attrString]; 
+0

"constructeur d'interface" ... – quemeful

+0

@quemeful Non vous ne pouvez pas définir, mais vous pouvez en mesure de définir via l'attribut d'exécution est possible. – Gowtham

1

Je ne sais pas pourquoi cela manque encore à NSButton. Mais voici la classe de remplacement Swift 4:

import Cocoa 

class TextButton: NSButton { 
    @IBInspectable open var textColor: NSColor = NSColor.black 
    @IBInspectable open var textSize: CGFloat = 10 

    public override init(frame frameRect: NSRect) { 
     super.init(frame: frameRect) 
    } 

    public required init?(coder: NSCoder) { 
     super.init(coder: coder) 
    } 

    override func awakeFromNib() { 
     let titleParagraphStyle = NSMutableParagraphStyle() 
     titleParagraphStyle.alignment = alignment 

     let attributes: [NSAttributedStringKey : Any] = [.foregroundColor: textColor, .font: NSFont.systemFont(ofSize: textSize), .paragraphStyle: titleParagraphStyle] 
     self.attributedTitle = NSMutableAttributedString(string: self.title, attributes: attributes) 
    } 
} 

enter image description here

enter image description here

+0

Ceci devrait être accepté comme réponse. Merci! – Nitesh

0

Vous pouvez également ajouter cette extension à votre code si vous aimez le « Jeter une extension et regarder si elle colle 'approche.

extension NSButton { 

    @IBInspectable open var textColor: NSColor? { 
    get { 
     return self.attributedTitle.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? NSColor 
    } 
    set { 
     var attributes = self.attributedTitle.attributes(at: 0, effectiveRange: nil) 
     attributes[.foregroundColor] = newValue ?? NSColor.black 
     self.attributedTitle = NSMutableAttributedString(string: self.title, 
                 attributes: attributes) 
    } 
    } 
}