2012-10-28 13 views
21

को रंग कैसे रंग दें मेरे पास एक सुंदर अंधेरे छवि वाला एनएसबटन है और दुर्भाग्यवश विशेषता निरीक्षक का उपयोग करके रंग बदलना संभव नहीं है। चित्र को बड़ा काला बटन देखें, पाठ पर जाएं।एनएसबीटन ओएसएक्स पर पाठ

enter image description here

एक संभावना पाठ रंग बदलने के लिए के लिए कोई सुराग? मैंने एनएसबटन कक्षा में देखा लेकिन ऐसा करने का कोई तरीका नहीं है। मुझे पता है कि मैं छवि को सफेद फ़ॉन्ट के साथ बना सकता हूं लेकिन यह वह नहीं है जो मैं करना चाहता हूं। स्विट्जरलैंड, रोनाल्ड Hofmann से

अभिवादन ---

+1

में करवा' setAttributedTitle की कोशिश की है? –

+0

अभी तक नहीं, एक मिनट प्रतीक्षा करें :) समान चीज़ कोई सेट नहीं है NSAttributedString में रंग। –

+0

'- [NSMutableAttributedString addAttribute: NSForegroundColorAttributeName मान: [NSColor colorWithSRGBRed: 1.0 हरा: 0.0 नीला: 0.0 अल्फा: 0.0] रेंज: NSMakeRange (0, string.length)];' –

उत्तर

38

यहाँ दो अन्य समाधान है: http://denis-druz.okis.ru/news.534557.Text-Color-in-NSButton.html

समाधान 1:

-(void)awakeFromNib 
{ 
    NSColor *color = [NSColor greenColor]; 
    NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitle]]; 
    NSRange titleRange = NSMakeRange(0, [colorTitle length]); 
    [colorTitle addAttribute:NSForegroundColorAttributeName value:color range:titleRange]; 
    [button setAttributedTitle:colorTitle]; 
} 

समाधान 2:

में * .एम फ़ाइल:

- (void)setButtonTitleFor:(NSButton*)button toString:(NSString*)title withColor:(NSColor*)color 
{ 
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; 
    [style setAlignment:NSCenterTextAlignment]; 
    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, style, NSParagraphStyleAttributeName, nil]; 
    NSAttributedString *attrString = [[NSAttributedString alloc]initWithString:title attributes:attrsDictionary]; 
    [button setAttributedTitle:attrString]; 
} 

-(void)awakeFromNib 
{ 
    NSString *title = @"+Add page"; 
    NSColor *color = [NSColor greenColor]; 
    [self setButtonTitleFor:button toString:title withColor:color]; 
} 
+0

कृपया अपना उत्तर संपादित करें और इसे पढ़ने योग्य बनाने के लिए कोड को प्रारूपित करें। – kleopatra

+0

कृपया इसे ठीक से प्रारूपित करें। इस तरह अन्य उपयोगकर्ता के लिए यह पोस्ट करना आसान है –

+0

किसी भी तरह से, यह मेरे हल समस्या, स्वरूपण की परवाह किए बिना। – jheld

7

ऐप्पल के पास Popover example के हिस्से के रूप में एनएसबटन के टेक्स्ट रंग को सेट करने के लिए कोड है।

नीचे दिए गए उदाहरण की जड़ (इस पोस्ट के लिए थोड़ा संशोधित, untested) है:

NSButton *button = ...; 
NSMutableAttributedString *attrTitle = 
    [[NSMutableAttributedString alloc] initWithString:@"Make Me Red"]; 
NSUInteger len = [attrTitle length]; 
NSRange range = NSMakeRange(0, len); 
[attrTitle addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:range]; 
[attrTitle fixAttributesInRange:range]; 
[button setAttributedTitle:attrTitle]; 

ध्यान दें कि fixAttributesInRange: करने के लिए कॉल महत्वपूर्ण (एक AppKit विस्तार) हो रहा है, लेकिन मैं नहीं मिल सकता है के रूप में प्रलेखन ऐसा क्यों है। एनएसबटन में जिम्मेदार तारों का उपयोग करने के साथ मेरे पास एकमात्र चिंता यह है कि यदि छवि को बटन (जैसे आइकन) के लिए भी परिभाषित किया गया है, तो जिम्मेदार स्ट्रिंग एक बड़े आयत पर कब्जा कर लेगी और छवि को बटन के किनारे पर धक्का देगी। दिमाग में कुछ सहन करना है।

अन्यथा ऐसा लगता है कि अपना खुद का drawRect: इसके बजाय ओवरराइड करना सबसे अच्छा तरीका है, जिसमें इस प्रश्न के दायरे से बाहर कई अन्य नुकसान हैं।

11

मेरे समाधान:

import Cocoa 

@IBDesignable 
class DVButton: NSButton 
{ 
    @IBInspectable var bgColor: NSColor? 
    @IBInspectable var textColor: NSColor? 

    override func awakeFromNib() 
    { 
     if let textColor = textColor, let font = font 
     { 
      let style = NSMutableParagraphStyle() 
      style.alignment = .center 

      let attributes = 
      [ 
       NSForegroundColorAttributeName: textColor, 
       NSFontAttributeName: font, 
       NSParagraphStyleAttributeName: style 
      ] as [String : Any] 

      let attributedTitle = NSAttributedString(string: title, attributes: attributes) 
      self.attributedTitle = attributedTitle 
     } 
    } 

    override func draw(_ dirtyRect: NSRect) 
    { 
     if let bgColor = bgColor 
     { 
      bgColor.setFill() 
      NSRectFill(dirtyRect) 
     } 

     super.draw(dirtyRect) 
    } 

} 

और स्विफ्ट 4:

.h 

IB_DESIGNABLE 
@interface DVButton : NSButton 

@property (nonatomic, strong) IBInspectable NSColor *BGColor; 
@property (nonatomic, strong) IBInspectable NSColor *TextColor; 

@end 


.m 

@implementation DVButton 

- (void)awakeFromNib 
{ 
    if (self.TextColor) 
    { 
     NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; 
     [style setAlignment:NSCenterTextAlignment]; 
     NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
              self.TextColor, NSForegroundColorAttributeName, 
              self.font, NSFontAttributeName, 
              style, NSParagraphStyleAttributeName, nil]; 
     NSAttributedString *attrString = [[NSAttributedString alloc]initWithString:self.title attributes:attrsDictionary]; 
     [self setAttributedTitle:attrString]; 
    } 
} 


- (void)drawRect:(NSRect)dirtyRect 
{ 
    if (self.BGColor) 
    { 
     // add a background colour 
     [self.BGColor setFill]; 
     NSRectFill(dirtyRect); 
    } 

    [super drawRect:dirtyRect]; 
} 

@end 

enter image description here

और यहाँ एक स्विफ्ट 3 संस्करण है।0 संस्करण:

import Cocoa 

@IBDesignable 
class Button: NSButton 
{ 
    @IBInspectable var bgColor: NSColor? 
    @IBInspectable var textColor: NSColor? 

    override func awakeFromNib() 
    { 
     if let textColor = textColor, let font = font 
     { 
      let style = NSMutableParagraphStyle() 
      style.alignment = .center 

      let attributes = 
      [ 
       NSAttributedStringKey.foregroundColor: textColor, 
       NSAttributedStringKey.font: font, 
       NSAttributedStringKey.paragraphStyle: style 
      ] as [NSAttributedStringKey : Any] 

      let attributedTitle = NSAttributedString(string: title, attributes: attributes) 
      self.attributedTitle = attributedTitle 
     } 
    } 

    override func draw(_ dirtyRect: NSRect) 
    { 
     if let bgColor = bgColor 
     { 
      bgColor.setFill() 
      __NSRectFill(dirtyRect) 
     } 

     super.draw(dirtyRect) 
    } 
} 
+0

क्या आप वें स्याही आप इस समाधान के लिए एक गोलाकार कोने जोड़ सकते हैं? :) – Yatko

+0

@Yatko बस NSRectFill (dirtyRect) को प्रतिस्थापित करें: rect = NSMakeRect (0, 0, bounds.width, bounds.height) rectanglePath = NSBezierPath (roundedRect: rect, xRadius: 3, yRadius: 3) आयताकार पाथ .fill() –

2

,, NSButton पर एक श्रेणी जोड़कर बस तुम क्या चाहते करने के लिए रंग निर्धारित करते हैं, और मौजूदा विशेषताओं preseve के बाद से शीर्षक केंद्रित किया जा सकता है गठबंधन आदि


छोड़ दिया
@implementation NSButton (NSButton_IDDAppKit) 

- (NSColor*)titleTextColor { 

    return [NSColor redColor]; 

} 

- (void)setTitleTextColor:(NSColor*)aColor { 

    NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedTitle]; 
    NSString* title = self.title; 
    NSRange range = NSMakeRange(0.0, self.title.length); 

    [attributedString addAttribute:NSForegroundColorAttributeName value:aColor range:range]; 
    [self setAttributedTitle:attributedString]; 
    [attributedString release]; 

} 

@end 
-2
NSColor color = NSColor.White; 
     NSMutableAttributedString colorTitle = new NSMutableAttributedString (cb.Cell.Title); 
     NSRange titleRange = new NSRange (0, (nint)cb.Cell.Title.Length); 
     colorTitle.AddAttribute (NSStringAttributeKey.ForegroundColor, color, titleRange); 
     cb.Cell.AttributedTitle = colorTitle; 
+0

क्या आप अपने समाधान की व्याख्या करने के लिए कुछ पाठ जोड़ सकते हैं? – Kmeixner

2

वास्तव में एक सरल, NSButton उपवर्गीकरण बिना पुन: प्रयोज्य समाधान:

[self setButton:self.myButton fontColor:[NSColor whiteColor]] ; 

-(void) setButton:(NSButton *)button fontColor:(NSColor *)color { 
    NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitle]]; 
    [colorTitle addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, button.attributedTitle.length)]; 
    [button setAttributedTitle:colorTitle]; 
} 
2

मैंने उप-वर्ग बनाया है जिसे FlatButton कहा जाता है जो इंटरफ़ेस बिल्डर के गुण निरीक्षक में टेक्स्ट रंग बदलने के लिए बहुत आसान बनाता है जैसे आप पूछ रहे हैं। यह आपकी समस्या के लिए एक सरल और व्यापक समाधान प्रदान करना चाहिए।

यह रंग और आकार जैसे अन्य प्रासंगिक स्टाइल विशेषताओं को भी उजागर करता है।

आप इसे यहाँ मिल जाएगा: `: https://github.com/OskarGroth/FlatButton

FlatButton for macOS

0

यह मैं कैसे यह स्विफ्ट 4

@IBOutlet weak var myButton: NSButton! 

// create the attributed string 
let myString = "My Button Title" 
let myAttribute = [ NSAttributedStringKey.foregroundColor: NSColor.blue ] 
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute) 
// assign it to the button 
myButton.attributedTitle = myAttrString