Is it possible to remove the Bold/Italic/Underline selection when highlighting and selecting a text in WKWebView?
Select a word, then press "Select"
The following dialog will present. Notice the "Bold/Italic/Underline" section. How do I remove this?
You should subclass WKWebView and override canPerformAction(_:withSender:) in your subclass.
The selector that displays the bold/italic/underline menu item is called _showTextStyleOptions: and it's an Objective-C method. The double parentheses prevent the compiler from showing a warning that says the method is not accessible.
import WebKit
class CustomWebView: WKWebView {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return action != Selector(("_showTextStyleOptions:"))
}
}
Related
// Subclass UITextView to remove default context menu items like Learn and Share and keep Cut, Copy and Paste
class TextView : UITextView {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
let actions = [#selector(copy(_:)), #selector(cut(_:)), #selector(paste(_:))]
return actions.contains(action)
}
}
I've used the above code to remove excessive menu items and only keep cut/copy/paste but recently there's a new menu item called Search Web that searches the highlighted text in Safari/Google. Is there a way to disable that too?
Remove it in buildMenu(with builder:)
override func buildMenu(with builder: UIMenuBuilder) {
if builder.menu(for: .lookup) != nil {
builder.remove(menu: .lookup)
}
super.buildMenu(with: builder)
}
In a custom UITextView, I override this function:
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
But it still shows two menuItems, "Open Link" and "Add to Reading list". I just want to show one "copy" item.
implement the method of UITextViewDelegate below this answer,retun an empty UIMenu that will disable all edit menu, you can try to only add the copy menu.
- (UIMenu *)textView:(UITextView *)textView editMenuForTextInRange:(NSRange)range suggestedActions:(NSArray<UIMenuElement *> *)suggestedActions API_AVAILABLE(ios(16.0)) {
return [UIMenu menuWithChildren:#[]];
}
When UITextView's allowsEditingTextAttributes property is enabled,
textView.allowsEditingTextAttributes = true
the textview can show the BIU (bold/italic/underlined) styling options in the context menu by UIMenuController.
UIMenuController - BIU Styling Options #1
UIMenuController - BIU Styling Options #2
I wonder how to add more styling options (e.g., strikethrough, highlight) to the context menu inside the BIU. For example, the iOS' native Notes app has four options (BIU + strikethrough) inside the styling menu.
BIU Styling Options in the native Notes app
Is there any way to do it? I have been spending hours to find a way to override the "Selector(("_showTextStyleOptions:"))" but couldn't find out how.. Please help me!!
When the editing menu is about to become visible, you get a canPerformAction(_:withSender:) call in your UITextView. This method is called again when the user selects a button within the menu. You can check if the font style button was selected and add a custom button to that submenu.
class MyTextView: UITextView {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
let menuController = UIMenuController.shared
if var menuItems = menuController.menuItems,
(menuItems.map { $0.action }).elementsEqual([.toggleBoldface, .toggleItalics, .toggleUnderline]) {
// The font style menu is about to become visible
// Add a new menu item for strikethrough style
menuItems.append(UIMenuItem(title: "Strikethrough", action: .toggleStrikethrough))
menuController.menuItems = menuItems
}
return super.canPerformAction(action, withSender: sender)
}
#objc func toggleStrikethrough(_ sender: Any?) {
print("Strikethrough button was pressed")
}
}
fileprivate extension Selector {
static let toggleBoldface = #selector(MyTextView.toggleBoldface(_:))
static let toggleItalics = #selector(MyTextView.toggleItalics(_:))
static let toggleUnderline = #selector(MyTextView.toggleUnderline(_:))
static let toggleStrikethrough = #selector(MyTextView.toggleStrikethrough(_:))
}
According to documentation, you may have to call update() on the UIMenuController after adding the button. But this was not necessary in my case.
This question already has answers here:
How to disable copy paste option from UITextField programmatically
(17 answers)
Closed 7 years ago.
I want to remove the option "Paste" from the selector
I've tried the code below but it adds other selections to the selector and the paste option is still there, it just disables the functionality
override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if action == "paste:" {
return false
}
return true
}
I want to remove Paste all together so the user doesn't even have the option to see it or click it
Have a look at UIMenuController
You should be able to use
var menuItems: [AnyObject]?
to set up your own object.
It is not a simple switch on or off the existing buttons, looks like you have to provide your own.
try this :
Step 1: You need to create another class which extends the UITextField. In this example, I made my CustomizedUITextField.
import UIKit
class CustomTextField: UITextField {
override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if action == "paste:" {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}
I just want to override a method in UITextView which is canPerformAction. In the overriden method I try to disable "paste:" action. In order to do that I tried to create a new type which will only handles the action not to done. All other uitextview related codes in the default ViewController.swift. So I just added a cocoa touch class in the project and set the target. Here is my custom UITextView's code as looks:
import UIKit
public class CustomTextView: UITextView {
override public func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if action == "paste:" {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}
When I select one of the uitextviews in the storyboard and view the Identity Inspector pane, I see that I m not able to set my custom class.
So what am I missing?
I've just tried your code and It's working. Just write: CustomTextView in your class in main.storyboard.