PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label uitextview. Show all posts
Showing posts with label uitextview. Show all posts

Tuesday, November 8, 2022

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

 November 08, 2022     edit, ios, menu, swift, uitextview     No comments   

Issue

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?


Solution

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



Answered By - Ike10
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, November 7, 2022

[FIXED] How to disable Share and Look up in UITextView

 November 07, 2022     menu, swift, uitextview     No comments   

Issue

I am trying to hide the text menus except for copy and two original menus.

I used the following code to suppress pretty much everything... but somehow, I can not hide look up and share (as well as Spell and Speak, added Jun 10).

  override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if  action == Selector(("_lookup:")) ||
        action == Selector(("_share:"))
    {
        return false
    } else  if
        action == #selector(UIResponderStandardEditActions.copy(_:)) ||
            action == #selector(copyAll(_:))  ||
            action == #selector(lookUpWord (_:))
    {
        return true
    } else {
    
    return false
    }
}

I debugged the code and witnessed that the function does return false when lookup and share went into the if-statement. But, somehow this is not reflected on the menu.

How can I make sure to disable look up, share, Spell, and Speak?

----- updated ----

Modified as extension But somehow still not working in the way I want. The menu does not reflect the code in this extension...

   extension UITextView {
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if  action == Selector(("_lookup:")) ||
                action == Selector(("_share:"))
        {
            return false
        } else  if
            action == #selector(UIResponderStandardEditActions.copy(_:)) ||
                action == #selector(ViewController.copyAll(_:))  ||
                action == #selector(ViewController.lookUpWord (_:))
        {
            return true
        } else {
            
            return false
        }
    }
}

Solution

  1. You can create subclass of UITextView with the custom implementation you want.

  2. To disable lookup, you need to use Selector (("_define:")).

Here is an example which disables Share & Lookup option :

class CustomTextView: UITextView {
    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        let canPerformAction = super.canPerformAction(action, withSender: sender)
        
        let shareAction = NSSelectorFromString("_share:")
        let lookUpAction = NSSelectorFromString("_define:")
        
        if canPerformAction && action == shareAction {
            // Set true/false based on your requirement
            return false
        }
        
        if canPerformAction && action == lookUpAction {
            return false
        }
        
        return canPerformAction
    }
}

And use this as:

class YourViewController: UIViewController {
    
    @IBOutlet weak var yourTextView: CustomTextView!
}

Edit:

And if you want to find the Selector for the Specific actions, Try this way:

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    print("TextView::canPerformAction: \(action)")
    return false
}

Select the action and this will print the associated action:

TextView::canPerformAction:_accessibilitySpeakLanguageSelection: TextView::canPerformAction: _accessibilityPauseSpeaking: TextView::canPerformAction: copy:

Or you can place a breakpoint and see what you're getting called with for "action".



Answered By - Kush Bhavsar
Answer Checked By - Clifford M. (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing