TomLisankie
TomLisankie

Reputation: 3955

How do I add a custom action to the text selection edit menu in iOS?

I need to add a custom action to the edit menu that pops up when a user selects some text in a UITextView in iOS.
How do I do this?

Upvotes: 14

Views: 12790

Answers (4)

dbala
dbala

Reputation: 366

since UIMenuItem was deprecated, either you must

  1. use editMenuInteraction solution
  2. use the editMenu function

Using editMenu function is straight forward. I've also posted my solution in: stackoverflow 73712955

private class CustomUITextView: UITextView, UIEditMenuInteractionDelegate {
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        // A little short-circuit for our new function to run.
        if action == #selector(printToConsole) {
            return true
        }
        return super.canPerformAction(action, withSender: sender)
    }
    
    override func editMenu(for textRange: UITextRange, suggestedActions: [UIMenuElement]) -> UIMenu? {
        let refreshAction = UIAction(title: "Print to Console") { (action) in
            self.printToConsole()
        }

        var actions = suggestedActions
        actions.insert(refreshAction, at: 0) // insert at the front of the menu.
        return UIMenu(children: actions)
    }
    
    @objc func printToConsole() {
       if let range = self.selectedTextRange, let selectedText = self.text(in: range) {
          print(selectedText)
       }
    }
}

Looks like this

solution photo of a menu with a 'print to console' showing up

and it works printing to console.

Upvotes: 2

Aryaa Sk
Aryaa Sk

Reputation: 99

Here is how you create a custom edit menu in swift 5:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var textfield: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let changeBackground = UIMenuItem(title: "Change Background Colour", action: #selector(changeBackgroundColour))
        UIMenuController.shared.menuItems = [changeBackground] //will add it to everything that has
    
    }

    @objc func changeBackgroundColour()
    {
        self.view.backgroundColor = .cyan //just makes the background colour cyan
    }

}

I have also made a youtube video explaining this here

Upvotes: 1

Greg432
Greg432

Reputation: 540

SWIFT 5

class ViewController: UIViewController, UITextViewDelegate {
    @IBOutlet weak var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

         addCustomMenu()
    }


    func addCustomMenu() {
       //Xcode doesn't like printToConsole being a var and a function call
       let printToConsole = UIMenuItem(title: "Print To Console", action: #selector(printToConsole2))

        UIMenuController.shared.menuItems = [printToConsole]
    }

    @objc func printToConsole2() {
       if let range = textView.selectedTextRange, let selectedText = textView.text(in: range) {
          print(selectedText)
       }
    }
}

Upvotes: 4

Ike10
Ike10

Reputation: 1595

class ViewController: UIViewController, UITextViewDelegate {

   @IBOutlet weak var textView: UITextView!

   override func viewDidLoad() {
      super.viewDidLoad()

      addCustomMenu()
   }

   func addCustomMenu() {
      let printToConsole = UIMenuItem(title: "Print To Console", action: #selector(printToConsole))
      UIMenuController.shared().menuItems = [printToConsole]
   }

   func printToConsole() {
      if let range = textView.selectedTextRange, let selectedText = textView.text(in: range) {
         print(selectedText)
      }
   }
}

This is an example of text selection menu item that changes the text in a UITextView to red. changeToRedFunc can perform any action you want.

Note: This is in Swift 3 (ask if you want it in Swift 2.3)

Hope this helps! If you have any questions feel free to ask! :D

Upvotes: 23

Related Questions