简体   繁体   中英

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.

该图显示了我如何使用检查器栏修改文本,但不能使用菜单,除了下划线

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. 在此输入图像描述
  3. Connect the menu items to addFontTrait(_:) action of the fontManager. Likewise, connect "Bigger" and "Smaller" items to modifyFont(_:) . 在此输入图像描述

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 .

If you ctrl drag from menu item to first responder, in menu view, you get the same options, just ctrl click. Then you implement whatever function you just connected. If you connect File > New to newDocument and implement in your ViewController

func newDocument(_ sender: Any?){
    print("func newDocument(_ sender: Any? \(String(describing: sender)))")
}

It will get called. First responder lists all the same methods as the added Object with NSFontManager as class. I don't use @IBAction in front of method because I don't connect it.

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