swiftmacosnstextviewnsdocumentnsresponder

Can underline, but not bold text in NSTextView


I have an NSDocuments & storyboard app created with the wizard in Xcode 8. In the ViewController Scene I have added a NSTextView. Certain menu items are disabled, such as Bold and Italic.

Looking at the First Responder the actions for bold and italic are not there. Am I supposed to write these methods myself? Is this due to the responder chain not being correctly set up? Why does underline show up but not bold?

Edit: Adding an image to show how I can edit text with the Inspector Bar, but the Format menu does not show the commands I would expect.

Image showing how I can modify text with the Inspector Bar, but not with the menu, apart from Underlining


Solution

  • There is a historical (?) reason of this problem. When the main menu was created in a xib file, xib file automatically contained a NSFontManager instance and such menu items like Bold were connected to it. However in a modern storyboard, there is no preset NSFontManager instance.

    Well then, you can connect them to a FontManager manually following the following steps.

    1. Create a normal Object instance (blue cube) in the Application scene.
    2. Change class of the Object instance to NSFontManager. enter image description here
    3. Connect the menu items to addFontTrait(_:) action of the fontManager. Likewise, connect "Bigger" and "Smaller" items to modifyFont(_:). enter image description here

    You also need to set menuItems' tag, however they are actually already set. Set the correspondent tag also manually only if menuItem's tag is 0.