简体   繁体   中英

Setting title on NSMenuItem, without effect

I'm getting an NSMenuItem from the Main Menu , with the code here : Getting NSMenuItem of NSMenu tree by title

However, something strange happens :

  • An NSMenuItem connected with an action : When using the sender property ( NSMenuItem ) and setting the title, it works .
  • BUT : When getting the item with the function above and set the title, the NSMenuItem's title does change, but the change is NOT reflected in the menu it belongs to.

What am I doing wrong? (I'm sure this one is really stupid... )

NSMenuItem* mi = [[core mainMenu] getItemWithPath:@"View" tag:PP_MENU_TAG_STATUSBAR];
[mi setTitle:@"newTitle"];
NSLog(@"mi : %@",[mi title]);

// mi changes, but no changes take effect in the mainMenu

I would forget the Getting NSMenuItem of NSMenu tree by title code and just connect each menu in IB.

Then use the setTitle on it when needed

UPDATE*

(see comments) It took me awhile to figure out why even my test one was not working!!. I had put in a attributed title within IB.

在此处输入图片说明

So when I later used setTitle . The property was being set but the actual displayed menu was overridden by the attributed title.

Removing the attributed title from IB. fixed this. And setTitle works as expected.

Also I have never used attributed title before. And I just pasted in some formatted coloured text in the IB attributed title. And the menu item was the same in colour and font.

Which I have always wanted to be able to do but thought was not possible.

And doing it programmatically is easy.

 NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"newTestMenu"];
    [string addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0,string.length)];

    [_testMenu setAttributedTitle:string];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM