2

I am developing an application with SwiftUI where i would like to insert an option in the menu that appears when text is selected, it was a custom item for this menu. menu

I want to know if it was possible to make this feature. thanks

ramesh sanghar
  • 37
  • 1
  • 1
  • 11
agmcoder
  • 195
  • 1
  • 8
  • hi, interesting, perhaps might be of interest https://stackoverflow.com/questions/37870889/how-do-i-add-a-custom-action-to-the-text-selection-edit-menu-in-ios – jspcal Nov 18 '22 at 23:05
  • 1
    the problem is that question is about UIKit intead of SwiftUI – agmcoder Nov 18 '22 at 23:40

1 Answers1

0

You need to use canPerformAction

for example if you only want to keep "copy", "Select all" and add the "comment" button

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            let menu = UIMenuController.shared
            let newInstanceItem = UIMenuItem(title: "Comment", action:#selector(reportAFault))
            menu.menuItems = [newInstanceItem]
            menu.update()
            if action == #selector(copy(_:)) || action == #selector(selectAll(_:)) || action == #selector(commentThisText){
                
                return true
            }
            return false            
    }
    

    
    @objc func commentThisText() {
            YOUR CODE
    }
dibs
  • 174
  • 1
  • 12