Skip navigation.
 
mlRe: NSImage in a NSMenuItem?
FROM : Olav Anderson
DATE : Thu Sep 30 22:24:15 2004

Greetings,


I tried this:

- (void)putImageInMenuItem:(NSMenuItem *)menuItem {
      NSFileWrapper *fileWrapper = [[[NSFileWrapper alloc] init] autorelease];
      [fileWrapper setIcon:[[[NSImage alloc] initWithContentsOfURL:[NSURL
URLWithString:@"http://www.wurldbook.com/favicon.ico"]] autorelease]];
      NSTextAttachment *textAttachment = [[[NSTextAttachment alloc]
initWithFileWrapper:fileWrapper] autorelease];
      //NSAttributedString *attString = [NSAttributedString
attributedStringWithAttachment:textAttachment];

  NSColor *txtColor = [NSColor redColor];
  NSFont *txtFont = [NSFont boldSystemFontOfSize:12];
  NSDictionary *txtDict = [NSDictionary dictionaryWithObjectsAndKeys:txtFont,
  NSFontAttributeName, txtColor,
NSForegroundColorAttributeName,textAttachment,
NSAttachmentAttributeName,nil];
  NSAttributedString *attString = [[[NSAttributedString alloc]
      initWithString:@"Hello!" attributes:txtDict] autorelease];

      [menuItem setAttributedTitle:attString];

}

and all I got was "Hello!" in black. no red or image.

Am I going about this wrong or is it not working for an app dock menu I wonder.

Best regards,

Olav


On Wed, 29 Sep 2004 15:09:52 -0700, John C. Randolph <<email_removed>> wrote:
>
>
>
> On Sep 29, 2004, at 2:59 PM, wURLdBook Research wrote:
>
> > Hi,
> >
> > I was trying to set a NSImage in a NSMenuItem in the DockMenu. I can't
> > seem get it to work even though I am doing all the normal stuffl like
> > [menutItem setImage:anImage].
> >
> > Is there a trick?
>
> On Sep 29, 2004, at 2:59 PM, wURLdBook Research wrote:
>
> Hi,
>
> I was trying to set a NSImage in a NSMenuItem in the DockMenu. I can't
> seem get it to work even though I am doing all the normal stuffl like
> [menutItem setImage:anImage].
>
> Is there a trick?
>
> The trick is to use an NSAttributedString with the image imbedded, and
> set that string as the menu item's title.
>
> -jcr
>
>
> John C. Randolph <<email_removed>> (408) 974-8819
> Sr. Cocoa Software Engineer,
> Apple Worldwide Developer Relations
> http://developer.apple.com/cocoa/index.html
>
>

Related mailsAuthorDate
mlNSImage in a NSMenuItem? wURLdBook Research Sep 29, 23:59
mlRe: NSImage in a NSMenuItem? John C. Randolph Sep 30, 00:09
mlRe: NSImage in a NSMenuItem? Olav Anderson Sep 30, 22:24
mlRe: NSImage in a NSMenuItem? Olav Anderson Oct 6, 17:09
mlRe: NSImage in a NSMenuItem? Andreas Mayer Oct 6, 17:49
mlRe: NSImage in a NSMenuItem? Olav Anderson Oct 6, 18:27