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.
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.