skip down to the 4 comments

Replicating Apples embossed text in a Cocoa App

[update] Thanks to the chaps below in the comments, my in-experience with cocoa is shown. This whole post could be replaced with this code:

 [[myTextField cell] setBackgroundStyle:NSBackgroundStyleRaised];

If you look at the text in the task or status bar on of a Mac app, you will see that the text has the appearance of being embossed slightly. It is essentially a textField with some neat attributes associated with it.

Apple embossed text

This effect does not come for free when you want to use your own NSTextField somewhere else in your Apps window (actually, I think it does on the iPhone?). To recreate this effect is quite simple. You just need to create a NSAttibutedString and associate the attributes below to it.

// Create the white shadow that sits behind the text
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[NSColor colorWithDeviceWhite:1.0 alpha:0.5]];
[shadow setShadowOffset:NSMakeSize(1.0, -1.1)];
// Create the attributes dictionary, you can change the font size
// to whatever is useful to you
NSMutableDictionary *sAttribs = [[[NSMutableDictionary alloc] initWithObjectsAndKeys:
    [NSFont systemFontOfSize:11.0],NSFontAttributeName,
    shadow, NSShadowAttributeName,
    nil] autorelease];
// The shadow object has been assigned to the dictionary, so release
[shadow release];
// Create a new attributed string with your attributes dictionary attached
NSAttributedString *s = [[NSAttributedString alloc] initWithString:@"82 results found"
// Set your text value
[myTextField setAttributedStringValue:s];
// Clean up
[s release];

I’m sure there are many other ways to achieve this effect, and I’d be interested in any easier versions, but this works for me.

There have been 4 responses to this post.

  1. Matt Gallagher

    Yes, on the iPhone you can use the same setShadowColor: and setShadowOffset: methods on a UILabel (equivalent of an NSTextField) without needing to set an attributed string. It’s not as easy as a setEmbossingEnabled:YES method might be but it is a couple lines shorter.

  2. Justin

    FYI, It’s much easier to do this:

    [myTextField setBackgroundStyle:NSBackgroundStyleRaised]

  3. Duncan

    Thanks Chaps, well and truly shamed :)

    I’m finding more and more that if something seems like a pain do in Cocoa, it’s because you haven’t looked hard enough through the API

  4. Bad Base

    Hi Duncan. If you are working pre 10.5 then this is a useful recipe. Thanks for posting it as it saved me some leg-work.

    Bad Base

back to the top