Re: NSMenuItem’s userKeyEquivalent lost if changing title
Re: NSMenuItem’s userKeyEquivalent lost if changing title
- Subject: Re: NSMenuItem’s userKeyEquivalent lost if changing title
- From: Allan Odgaard <email@hidden>
- Date: Tue, 20 Sep 2016 09:37:14 +0200
On 20 Sep 2016, at 9:11, Dave Lyons wrote:
(Ooh! I know that one!)
The custom shortcut for Finder's File > Compress menu item continues
to work, because Finder goes slightly out if its way to achieve it.
The item's -title remains unchanged as ”Compress”, even when you
see "Compress “foo”" or "Compress 42 Items” -- in that case,
you're seeing the -attributedTitle. (Ditto for "Burn to Disc…”
item.)
Thanks, I’ll switch to using this category method for setting dynamic
titles:
```Objective-C
- (void)setDynamicTitle:(NSString*)plainTitle
{
if(self.userKeyEquivalent && ![self.userKeyEquivalent
isEqualToString:@""])
{
NSString* title = plainTitle;
plainTitle = self.title;
NSFont* font = self.menu.font ?: [NSFont menuFontOfSize:0];
self.attributedTitle = [[NSAttributedString alloc]
initWithString:title attributes:@{ NSFontAttributeName : font }];
}
self.title = plainTitle;
}
```
Small downside: If you look carefully while the menu item is disabled,
it looks a bit different from other disabled menu items.
Yeah, definitely not a fan of attributed strings in menu items for this
reason, but seeing as this is how Finder (hence Apple) achieves it, I
guess there are no better solution, and with the above code, only users
with custom app shortcuts should be affected.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden