Skip to main content

Adding Items to NSTextView’s Contextual Menu

·3 mins

When you add a text view to your application using Interface Builder you get access to the text view’s default contextual menu. You can open the contextual menu in your application by right-clicking (or control-clicking) in the text view. This menu lets you do things like cut, copy, paste, and check the document for spelling errors. If you’ve read Aaron Hillegass’s Cocoa programming book you know how to replace the default contextual menu, but you may want to use the default menu and add some menu items to it. If you’re writing an HTML editor, you may want to add a menu item to wrap HTML tags around selected text. This post shows you how to add a menu item to NSTextView’s contextual menu.

Accessing the Contextual Menu #

The first step to adding an item to a text view’s contextual menu is to access the menu. You can access NSTextView’s default contextual menu using the menu: method.

IBOutlet NSTextView* textView;
NSMenu* textViewContextualMenu = [textView menu];

Adding a Menu Item to the Contextual Menu #

After you get access to the contextual menu, call NSMenu’s addItemWithTitle: method to add a menu item to the contextual menu. You must supply three pieces of information: the name of the menu item, the action, and the keyboard equivalent for the menu item. The action is the method that gets called when the user chooses the menu item. The following example adds a Tags menu item to the contextual menu with no keyboard equivalent:

- (IBAction)createTag:(id)sender;
NSMenuItem* tagsMenuItem = [textViewContextualMenu addItemWithTitle:@"Tags"
	action:@selector(createTag:) keyEquivalent:@""];

If you want to place a menu item in a specific place in the contextual menu, call NSMenu’s insertItemWithTitle: method. This method works similarly to the addItemWithTitle: method, but there is an additional argument to specify: the index (location in the menu) where you want to insert the menu item.

Adding a Submenu #

Sometimes you need to add a submenu to a menu item. In the example I’ve been using in this article, you may decide to add a menu of tags as a submenu of the Tags menu so the user can add a specific tag. Call the contextual menu’s setSubmenu: method to add a submenu. Supply the menu to add and the menu item where you’re adding the menu. The following example adds a menu of tags to the Tags menu item:

IBOutlet NSMenu* tagsMenu;
[textViewContextualMenu setSubmenu:tagsMenu forItem:tagsMenuItem];

The example assumes you created a menu in Interface Builder. Add a NSMenu object to the xib file, and add items to the NSMenu object. Creating a menu in Interface Builder is generally easier than creating a menu programmatically, but you can create the menu in your source code if you want. Refer to the NSMenu Class Reference, which is part of Apple’s documentation, to learn more about creating a menu in code. Searching for NSMenu in Xcode’s documentation window should be enough to find the class reference.