Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Attributed Titles for Buttons

Attributed Titles for Buttons
Thread Tools
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Jul 2, 2001, 02:54 AM
 
Hello ...

I'm trying to make a button show its title in white, but this simplest of tasks seems beyond me.

Here's one incarnation of my code (assuming no typos):

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
NSMutableAttributedString *titleString;
NSMutableDictionary *titleAttr;

<font color = brown>// Set up dictionary of text attributes.</font>
titleAttr = [[NSMutableDictionary dictionaryWithCapacity:<font color = blue>0</font>] retain];
[titleAttr setObject:[NSFont fontWithName:<font color = orange>@"Impact"</font> size:<font color = blue>18.0</font>]
forKey:NSFontAttributeName];
[titleAttr setObject:[NSColor whiteColor]
forKey:NSForegroundColorAttributeName];

<font color = brown>// Set up attributed string <font color = green>for</font> title.</font>
[titleString initWithString:<font color = orange>@"blah"</font> attributes:titleAttr];

[myButton setAttributedTitle:titleString];
</font>[/code]

This generates a 'SIGSEGV' error, as do all my variations on this theme. Strings have always given me more trouble than I like to admit, but I thought this would have been fairly straightforward.

Help?

Thanks,

DayLateDon
     
Mac Elite
Join Date: May 2001
Status: Offline
Reply With Quote
Jul 2, 2001, 06:49 AM
 
You can't use styled text for buttons, they are drawn by the Appearance manager and it expects plain thext to draw it in the current theme to ensure a consistent look. And your white text would not be visible if Apple decided one day to release a theme with white buttons.

If you really need white text in a button you have to make an entire control yourself, but it's no good style not to use standard controls without a good reason.

-
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Jul 2, 2001, 09:01 AM
 
Hello ...

If we weren't supposed to set attributed titles for buttons, then why is there a "setAttributedTitle:" method? (The NSButton class even has defined "setAttributedAlternateTitle:", "attributedTitle", and "attributedAlternateTitle"!) It's bad enough that the developer documentation doesn't describe methods that *do* exist; are you saying now that it *does* describe methods that *don't* exist?

The buttons I'm creating don't threaten to conflict with Appearance Manager concerns; they aren't gel-cap push buttons or bevel buttons. They're just clickable icon-buttons (each a "square button" with attributes "momentary light" and "not-bordered", displaying a custom icon). The icons happen to be somewhat darkish, so black titles (which I've positioned as "overlapping" the image) are too low-contrast to be readable.

I *can* create completely custom controls, but it seems to me as though the functionality I need is (or, if not, then should be) built into the NSButton API already.


--DayLateDon
     
Mac Elite
Join Date: May 2001
Status: Offline
Reply With Quote
Jul 2, 2001, 09:47 AM
 
Oh, first I thought you meant a Push Button

And yes, sometimes the documentation doesn't describe existing things and vice versa. Does your code work with setTitle instead of setAttributedTitle? If yes, be sure it's not implemented. I think Apple put the docs quickly together to have at least something and had no time for revising them yet. Looking at the NSButton class there are some combinations of methods which would not make many sense... getting some error code would be fine

But if you're overlapping an icon, why don't you create icons with your text "built in"?

-
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Jul 2, 2001, 11:04 AM
 
Try the following instead:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
NSAttributedString *titleString;
NSMutableDictionary *titleAttr;

<font color = brown>//Set up dictionary of text attributes.</font>
titleAttr = [NSMutableDictionary dictionary];
[titleAttr setObject:[NSFont fontWithName:<font color = orange>@"Impact"</font> size:<font color = blue>18.0</font> forKey:NSFontAttributeName];
[titleAttr setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];

<font color = brown>//Set up attributed string <font color = green>for</font> title.</font>
titleString = [[NSAttributedString alloc] initWithString:<font color = orange>@"blah"</font> attributes:titleAttr];
[myButton setAttributedTitle:titleString];
[titleString release];
</font>[/code]

Your original code was using an uninitialized variable (titleString), which is what was causing the segfault. I'm surprised the compiler didn't catch that. You need to create the object, which is done with [[NSAttributedString alloc] init...] in this case. I couldn't see any reason why it needed to be an NSMutableAttributedString so I changed it.

You didn't need to retain the NSMutableDictionary. It's already autoreleased (the +dictionary method is short for [[[NSMutableDictionary alloc] init] autorelease]), and will go away at the end of the user event. If you need to keep it beyond that point, i.e. if it was an instance variable of your class, then you'd need to retain it, but as it's just a local variable you don't. Similarly, when using an +alloc method explicitly, you have to balance it with either a -release or -autorelease.

[ 07-02-2001: Message edited by: lindberg ]
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Jul 2, 2001, 12:16 PM
 
Hello ...

Thanks for the additional insights.

I still haven't wrapped my mind around the intricacies of 'alloc', 'release', and all that, so it doesn't surprise me that my ignorance there was the root of my problem. Usually the compiler slaps me in the face, but not this time.

As for the Mutable String and retained Mutable Dictionary: I *think* I'm right in using them (although I'll never be sure in my own head). The button titles will change as the program runs, so they need to be mutable. And I figure I need to keep the 'titleAttr' Dictionary around (I really have it declared as an instance variable in my controller class) so that I don't have to reallocate it and reset its keys every time I want to change those button names (which will happen often).


Thanks again ...

DayLateDon
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:43 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2