Note that there are some explanatory texts on larger screens.

plurals
  1. PONSString *text to NSString *icon?
    primarykey
    data
    text
    <p>I am making an app that is a standalone menu item and the basis for the code is sample code I found on a website. The sample code uses a number as the menu icon, but I want to change it to an image. </p> <p>I want it to be like other apps where it shows <code>icon.png</code> when not clicked and <code>icon-active.png</code> when clicked.</p> <p>The current code is this:</p> <pre><code>- (void)drawRect:(NSRect)rect { // Draw background if appropriate. if (clicked) { [[NSColor selectedMenuItemColor] set]; NSRectFill(rect); } // Draw some text, just to show how it's done. NSString *text = @"3"; // whatever you want NSColor *textColor = [NSColor controlTextColor]; if (clicked) { textColor = [NSColor selectedMenuItemTextColor]; } NSFont *msgFont = [NSFont menuBarFontOfSize:15.0]; NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init]; [paraStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]]; [paraStyle setAlignment:NSCenterTextAlignment]; [paraStyle setLineBreakMode:NSLineBreakByTruncatingTail]; NSMutableDictionary *msgAttrs = [NSMutableDictionary dictionaryWithObjectsAndKeys: msgFont, NSFontAttributeName, textColor, NSForegroundColorAttributeName, paraStyle, NSParagraphStyleAttributeName, nil]; [paraStyle release]; NSSize msgSize = [text sizeWithAttributes:msgAttrs]; NSRect msgRect = NSMakeRect(0, 0, msgSize.width, msgSize.height); msgRect.origin.x = ([self frame].size.width - msgSize.width) / 2.0; msgRect.origin.y = ([self frame].size.height - msgSize.height) / 2.0; [text drawInRect:msgRect withAttributes:msgAttrs]; } </code></pre> <p>Also, I found a post describing a method on how to do this, but it did not work for me. The url to that is this: <a href="http://mattgemmell.com/2008/03/04/using-maattachedwindow-with-an-nsstatusitem/comment-page-1#comment-46501" rel="nofollow">http://mattgemmell.com/2008/03/04/using-maattachedwindow-with-an-nsstatusitem/comment-page-1#comment-46501</a>.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload