NSButton Setting Textcolor
Mac OS X (10.5.6)
Mac OS X (10.5.6)
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];
- (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];
}
NSButton Setting Textcolor