• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to set the color of a string on a NSButton?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to set the color of a string on a NSButton?


  • Subject: Re: How to set the color of a string on a NSButton?
  • From: David Hinz <email@hidden>
  • Date: Thu, 23 Nov 2006 11:17:29 -0700


Thanks for the responses. I think I found a way to do it (and there may be a memory leak, but I'll fix that up).


Here is what I used:

// Center the label on the button
NSMutableParagraphStyle *ps = [[[NSMutableParagraphStyle alloc] init] autorelease];
[ps setAlignment:NSCenterTextAlignment];


// Set the color and paragraph style
NSDictionary *textDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSColor whiteColor], NSForegroundColorAttributeName, ps, NSParagraphStyleAttributeName, nil];


NSMutableAttributedString *buttonString = [[[NSMutableAttributedString alloc] initWithString:@"3" attributes:textDictionary] autorelease];
[threeButton setAttributedTitle:buttonString];



The reason I'm changing the color of the text is that I'm creating a calculator app and trying to match the look of an old calculator I have. Some of the buttons have a brown background with white text.


I'm still surprised that InterfaceBuilder doesn't have a way to change the font, colors, etc. of the attributed string on a NSButton.

david.




On Nov 23, 2006, at 12:16 AM, Nick Zitzmann wrote:


On Nov 23, 2006, at 12:10 AM, David Hinz wrote:

The error I got at run time is:
-[NSCFString setAttributes:range:]: selector not recognized [self = 0x34df00]


This is what I tried:

NSDictionary *textDictionary = [NSDictionary dictionaryWithObject: [NSColor whiteColor]
forKey:NSForegroundColorAttributeName];
[(NSMutableAttributedString *)([[threeButton stringValue] mutableCopy]) setAttributes:textDictionary
range:NSMakeRange(0, [[threeButton stringValue] length])];

The exception is to be expected, since -stringValue returns an NSString, not an NSAttributedString. But even if you used - attributedStringValue your code does nothing but leak memory, since it's operating on a copy of the string and doesn't set the copy into the control, and the string isn't being released.


There must be an easier way to set the text color on a NSButton.

Try -attributedStringValue, but what are you trying to accomplish? Changing the color of a string in an NSButton seems a little bit non-standard to me.


Nick Zitzmann
<http://www.chronosnet.com/>







_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins (at) lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >How to set the color of a string on a NSButton? (From: David Hinz <email@hidden>)
 >Re: How to set the color of a string on a NSButton? (From: Nick Zitzmann <email@hidden>)

  • Prev by Date: Simulate Context Menu with Window
  • Next by Date: Re: Leak Hunting and Object Alloc
  • Previous by thread: Re: How to set the color of a string on a NSButton?
  • Next by thread: Re: How to set the color of a string on a NSButton?
  • Index(es):
    • Date
    • Thread