Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
I'm trying to get my NSMenuItem to have the exact same font/style as the default ones, but I'm having troubles.

Here's my code:
Code:
NSDictionary *attributeDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"LucidaGrande" size:13.0], NSFontAttributeName, nil];
statusItem.attributedTitle = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" Mon Feb 6"] attributes:attributeDictionary];

I've attached the output below. On the far left is my menu item. On the far right is the built in date / time menu item I'm trying to copy.

It's close, but not quite right. It's what I was told was the proper font, but it actually looks to me like what was the proper font several OS versions ago (10.4?) It looks a little too sharp... it should be slightly taller and slightly wider. Making it the next size up makes it too tall. Making it bold... actually, I haven't figured out how to bold it (I've tried adding "-bold" to the end of the name of the font, but that seems to result in getting a completely unrelated font to show up...)

Edit: Adding a shadow with offset of 0.5, 0.5 seems a little promising... it doesn't look quite right, though...
 

Attachments

  • Font.png
    Font.png
    6.9 KB · Views: 412
Last edited:
I played around with a few ideas, and this seems to get the best results. It seems turning off CGContextSetShouldSmoothFonts() makes it look better.

Code:
[color=#5c2699]NSTextFieldCell[/color] *cell = [[[[color=#5c2699]NSTextFieldCell[/color] [color=#2e0d6e]alloc[/color]] [color=#2e0d6e]initTextCell[/color]:text] [color=#2e0d6e]autorelease[/color]];
[cell [color=#2e0d6e]setDrawsBackground[/color]:[color=#aa0d91]NO[/color]];
[cell [color=#2e0d6e]setFont[/color]:font];
[cell [color=#2e0d6e]setBackgroundStyle[/color]:[color=#2e0d6e]NSBackgroundStyleRaised[/color]];

[color=#5c2699]NSSize[/color] cellSize = [cell [color=#2e0d6e]cellSize[/color]];
[color=#5c2699]NSSize[/color] imgSize = [color=#2e0d6e]NSMakeSize[/color]([color=#2e0d6e]floor[/color](cellSize.[color=#5c2699]width[/color]), [[[color=#5c2699]NSStatusBar[/color] [color=#2e0d6e]systemStatusBar[/color]] [color=#2e0d6e]thickness[/color]]);
[color=#5c2699]NSImage[/color] *img = [[[color=#5c2699]NSImage[/color] [color=#2e0d6e]alloc[/color]] [color=#2e0d6e]initWithSize[/color]:imgSize];
[img [color=#2e0d6e]lockFocus[/color]];
[color=#5c2699]NSRect[/color] bounds = [color=#2e0d6e]NSMakeRect[/color]([color=#1c00cf]0[/color], [color=#1c00cf]0[/color], imgSize.[color=#5c2699]width[/color], imgSize.[color=#5c2699]height[/color]);
[color=#5c2699]CGContextRef[/color] ctx = ([color=#5c2699]CGContextRef[/color])[[[color=#5c2699]NSGraphicsContext[/color] [color=#2e0d6e]currentContext[/color]] graphicsPort];
[color=#2e0d6e]CGContextSaveGState[/color](ctx);
[color=#2e0d6e]CGContextSetShouldSmoothFonts[/color](ctx, [color=#643820]false[/color]);
[color=#5c2699]NSRect[/color] cellRect = [color=#2e0d6e]NSMakeRect[/color]([color=#1c00cf]0[/color], (bounds.[color=#5c2699]size[/color].[color=#5c2699]height[/color] - cellSize.[color=#5c2699]height[/color]) / [color=#1c00cf]2[/color], cellSize.[color=#5c2699]width[/color], cellSize.[color=#5c2699]height[/color]);
[cell [color=#2e0d6e]drawWithFrame[/color]:cellRect [color=#2e0d6e]inView[/color]:[color=#aa0d91]nil[/color]];
[color=#2e0d6e]CGContextRestoreGState[/color](ctx);
[img [color=#2e0d6e]unlockFocus[/color]];

[statusItem [color=#2e0d6e]setImage[/color]:img];
 
I'm surprised Apple hasn't actually told developers how to make text that fits in with the rest of the status items... thanks for your help, I think this is going to work 🙂
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.