Posted By

mirage3d on 12/20/11


Tagged

cocoa mac Objective-c NSButton NSMutableParagraphStyle NSAttributedString


Versions (?)

Change Color of NSButton


 / Published in: Objective C
 

URL: https://discussions.apple.com/message/9334854#9334854

A Modular method to set new text with color of an NSButton. Thanks go to the owner of this code - RayNewbie - in the Apple discussion forum.

  1. - (void)setButtonTitleFor:(NSButton*)button toString:(NSString*)title withColor:(NSColor*)color {
  2. [style setAlignment:NSCenterTextAlignment];
  3. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  4. color, NSForegroundColorAttributeName, style, NSParagraphStyleAttributeName, nil];
  5. NSAttributedString *attrString = [[NSAttributedString alloc]
  6. initWithString:title attributes:attrsDictionary];
  7. [button setAttributedTitle:attrString];
  8. [style release];
  9. [attrString release];
  10. }

Report this snippet  

You need to login to post a comment.