skip to Main Content

Working with UITextView

//...
txtView.isFindInteractionEnabled = true
//...

My code:

override func buildMenu(with builder: UIMenuBuilder) {
    if #available(iOS 16.0, *) {
        builder.remove(menu: .lookup)
    }
    super.buildMenu(with: builder)
}

This will remove: Find selection, look up, translate, search web.

I want to keep only the option: "Find selection" and remove the others "look up, translate, search web"

2

Answers


  1.     override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            
            if action.description == "_findSelected:" {
                return true
            }else{
                return false
            }
        }
    
    Login or Signup to reply.
  2. Instead of removing the whole menu you can remove specific child elements from it.

    override func buildMenu(with builder: UIMenuBuilder) {
        if #available(iOS 16.0, *) {
            builder.replaceChildren(ofMenu: .lookup) { elements in
                return elements.filter { item in
                    switch (item as? UICommand)?.action.description {
                    case "_define:", "_translate:":
                        return false
                    default:
                        return true
                    }
                }
            }
        }
        super.buildMenu(with: builder)
    }
    

    _define: action contains both the elements Look up and Search web.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search