How to Change the text Color of a NSButton in Objective-C
One of the things that I need to do is simply change the text of a NSButton (or NSButtonCell) to a different color. No problem I thought. I can easily do this in any language since I started developing Windows 3.1 Applications.
Boy was I wrong. There is no simple way to do it.
Before the Apple purist start yelling that I should not do it because the guidelines state not do, I need to do it for the game I am developing. Normally I would 100% agree and I think that is what makes Apple apps so great is that they look consistent and that is part of the problem with Windows App: plagued by horribly colored apps.
After trying to Google and looking though all the docs from Apple to find a solution with not much help, I was finally able to find a couple of posting that pointed me to the right direction. A little from this post, a little direction from another I was finally able find a solution.
Here is the code snippet from my test. It’s not perfect by no means but you get the idea of how to do it.
- (IBAction)section1Selected:(id)sender
{
[section2 deselectAllCells];
NSLog (@"Selected");
id cell = [section1 selectedCell];
if (cell == nil)
NSLog(@"Nothing Selected");
else
{
NSButtonCell *button = (NSButtonCell *)cell;
NSString *myString = [button title];
NSLog(@"The Button Text is %@", myString);
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
[pStyle setAlignment:NSCenterTextAlignment];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSColor blueColor],
NSForegroundColorAttributeName,
[button font], NSFontAttributeName,
pStyle, NSParagraphStyleAttributeName, nil];
NSAttributedString *atted = [[NSAttributedString alloc] initWithString:myString attributes:dict];
[button setAttributedTitle: atted];
}
}
This code has a NSMatrix that contain 9 NSButtonCells. It finds the one that was selected in the NSMatrix and then I create a NSDictionary where I add 3 value key pairs, the NSForegroundColorAttributeName, the NSFontAttributeName and the NSParagraphStyleAttributeName.
I then create a NSAttributeString and initializde it with the text of the NSButtonCell and the attributesI created from the NSDictionary object. Finally I set the AttributedTitle to the NSAttributedString.
WHEW! That is a ton of work just to get the color of the text of a button changed.
One thing I did not talk about was the third value key pair I added to the NSDictionary. Before I added this, the text of the button would change the color, but it would be aligned to the left. Even though I passed in the Font attribute, it seemed to ignore this. I’m not sure if its a bug or not but its probably not been looked at by Apple because I have to believe this is a hack and not a recommended solution. Either way I created a NSMutableParagraphStyle object, set it to align in the middle and then added it to the NSDictionary object which was set as the attributes to the button. WOOT! That worked!
Hopefully this post will help new Cocoa developers who are looking at changing the text of the button. However its not intended for normal apps and you can’t blame me if all the Mac Apps buttons have changed color.
I wanted to show that with a little digging into the API, you’re capable of doing anything! Even something I’ve taken for granted for 15 years!!