Dr.Kameleon
Dr.Kameleon

Reputation: 22810

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 :

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

Upvotes: 0

Views: 2368

Answers (1)

markhunte
markhunte

Reputation: 6932

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.

enter image description here

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];

Upvotes: 1

Related Questions