Nunian Soong
Nunian Soong

Reputation: 11

How do I add a custom contextual menu item to UITextView using Swift?

I managed to subclass UITextView and disabled the "Define" contextual menu item.

class TextViewer: UITextView {

   // Overide, disable the "Define" contextual menu item
   override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool     {
       if action == "_define:" {
           return false;
       }
       return super.canPerformAction(action, withSender: sender)
   }

}

But now, how do I add a different/custom menu item to take its place? I was guessing it might be something like this, but it's not quite right.

var menuController = UIMenuController.sharedMenuController()
var customMenuItem = UIMenuItem(title: "Lookup", action: "lookupWord")
menuController.menuItems?.append(customMenuItem)

Thanks for any help you folks can give me. ;-)

Upvotes: 0

Views: 4443

Answers (1)

Nunian Soong
Nunian Soong

Reputation: 11

Nevermind:

let mnuController = UIMenuController.sharedMenuController()
let lookupMenu = UIMenuItem(title: "Tshawb", action: "tshawb")
mnuController.menuItems = NSArray(array: [lookupMenu])

// This makes the menu item visible.
mnuController.setMenuVisible(true, animated: true)

Upvotes: 1

Related Questions