I change the mainMenu loading a new MainMenu from xib.
I call the newMenu in place of existing one as follows:
Bundle.main.loadNibNamed("newMainMenu", owner: self, topLevelObjects: nil)
I get the new menu.
But the First item (which usually holds the app name) is plain, I would need it bold.
Is that even possible with a menu title different from the app's name ?
I've tried whatever I could imagine without success.
Notably:
let items = NSApplication.shared.mainMenu?.items
let firstItem = items?[0]
let subMenu = firstItem?.submenu
subMenu?.title = "NewApplication " // Only this can change the menu title
subMenu?.font = NSFont.boldSystemFont(ofSize: 0)
In fact, detailed situation is :
- original first item of mainmenu is "MyApp"
- new menu title in xib is "NewApplication"
If I just call
Bundle.main.loadNibNamed("newMainMenu", owner: self, topLevelObjects: nil)
I get the new menu (submenus are different), but title of the first remains "MyApp" and is bold
If I add:
let items = NSApplication.shared.mainMenu?.items
let firstItem = items?[0]
let subMenu = firstItem?.submenu
subMenu?.title = "NewApplication"
I get the new menu bar but title of first item remains MyApp.
However, if I change a bit adding a " " to the name
let items = NSApplication.shared.mainMenu?.items
let firstItem = items?[0]
let subMenu = firstItem?.submenu
subMenu?.title = "NewApplication " // ADDED " " at the end, name is different
I get the new menu (submenus are different), title is now "NewApplication" but plain, not bold
I tried to add:
subMenu?.font = NSFont.boldSystemFont(ofSize: 0)
No change, still plain
Also tried:
var attributes = [NSAttributedString.Key: Any]()
let boldFont = NSFont.boldSystemFont(ofSize: 0)
attributes[NSAttributedString.Key.font] = boldFont
firstItem?.attributedTitle = NSAttributedString(string: "NewApplication ", attributes: attributes)
With no result: still plain, not bold.
I come to the conclusion that when OSX recognizes the name of the app it turns title bold. Otherwise, cannot be bold, just as any other menu.
Only option would be to change the app name, which is a bit extreme solution.
Read this SO, no solution there either.
https://stackoverflow.com/questions/4965466/set-titles-of-items-in-my-apps-main-menu
Am I right ?