NSButton Setting Textcolor

Am just starting with development on Mac OS. Am interesting in knowing about the ways to set the text color for NSButton. I have used Xcode and Interface Builder for creating a simple application with a button control. Any suggestions are welcome.

Mac OS X (10.5.6)

Posted on Apr 13, 2009 8:38 AM

Reply
6 replies

Apr 13, 2009 2:38 PM in response to McBgnr

Although the title color for UIButton is set in one simple method, this task is quite a bit more complex in NSButton. Here are links to the methods and the constant you'll need:

- (void)setAttributedTitle:(NSAttributedString *)aString

- (id)initWithString:(NSString *)aString attributes:(NSDictionary *)attributes

NSForegroundColorAttributeName

Let me know if you need help writing the code after looking over the above, ok?

Apr 13, 2009 8:14 PM in response to RayNewbie

Thanks... the links are so helpful...

However, this is my very first program in Mac OS and I am not sure how to write code for this.

I have added a button to the application window using Interface Builder and have added an application controller (Objective C Class) to the mainmenu.xib file. How shall I proceed with my program to get this working.

Suggestions are welcome.

Apr 13, 2009 8:21 PM in response to McBgnr

Here's the code to set the button title color in a controller class which connects to the button with an IBOutlet:

NSColor *color = [NSColor greenColor];
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];
[self.button setAttributedTitle:attrString];
[style release];
[attrString release];

I'm assuming you want the title text to be centered, so this attribute is also set above, since the attributed string will otherwise default to left justified.

Apr 16, 2009 3:13 AM in response to RayNewbie

This works great RayNewbie. Thanks a lot.

Now that this is working... I was thinking of putting this into a function which will accept a pointer to NSButton and change its color. How can I write such a function? I tried to look in the net but could not find something for declaring and calling such a custom function. Inputs will be appreciated.

Apr 16, 2009 4:16 PM in response to McBgnr

The task of parameterizing a block of code to make a reusable method (or function) is a basic programming topic, not something you'll learn by searching the web for sample code. I would recommend getting a good book on the fundamentals of programming and then a book on Obj-C programming in particular.

Here's how the code I posted for you above is changed to a method along with some sample code to call the method:

- (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];
[style release];
[attrString release];
}
-(IBAction)clickOnMe:(id)sender {
NSArray *colorArray = [[NSArray alloc] initWithObjects:
[NSColor blackColor],
[NSColor redColor],
[NSColor greenColor],
[NSColor blueColor],
nil];
NSColor *color = [colorArray objectAtIndex:random()%4];
[self setButtonTitleFor:sender toString:@"Click On Me" withColor:color];
[colorArray release];
}

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

NSButton Setting Textcolor

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.