How to disable UITextview selection text, copy/paste UIMenuController but still have hyperlinks working [Not duplicate]
just try to create a subclass of UITextView that overrides the canPerformAction:withSender:
method
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:))
return NO;
return [super canPerformAction:action withSender:sender];
}
How to disable pasting in a TextField in Swift?
I agree with Leonardo Savio Dabus, if I were you I'd use string checking and just give out a warning, it makes things easier. BUT, if disabling paste option is a fancy feature you really want to put into your app, then you need to do more work. I'll provide the steps below.
Step 1: You need to create another class which extends the UITextField
. In this example, I made my CustomUITextField
.
import Foundation
import UIKit //Don't forget this
class CustomUITextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}
Step 2: Wire the storyboard with your ViewController. You need to declare an IBOutlet
as in normal case:
@IBOutlet var textFieldA: CustomUITextField?
Wire the circle next to the @IBOutlet
to the TextField
in the storyboard. THEN, this is important and easy to be ignored:
- Go to your storyboard
- Click the target
TextField
- Select Identity Inspector (the 3rd one)
- Change the class to
CustomUITextField
Quick snapshot is provided below.
That's it, hope this works.
Credit:
Main reference
If you want to know more about the behavior of canPerformAction
method, though it's an Objective-C version, the concepts are shared here.
How do you REALLY remove Copy from UIMenuController
The technique you linked to still seems to work. I implemented a UIWebView
subclass with these methods, and only the A and B items appeared.
+ (void)initialize
{
UIMenuItem *itemA = [[UIMenuItem alloc] initWithTitle:@"A" action:@selector(a:)];
UIMenuItem *itemB = [[UIMenuItem alloc] initWithTitle:@"B" action:@selector(b:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:itemA, itemB, nil]];
[itemA release];
[itemB release];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
BOOL can = [super canPerformAction:action withSender:sender];
if (action == @selector(a:) || action == @selector(b:))
{
can = YES;
}
if (action == @selector(copy:))
{
can = NO;
}
NSLog(@"%@ perform action %@ with sender %@.", can ? @"can" : @"cannot", NSStringFromSelector(action), sender);
return can;
}
Related Topics
Storing [Nsnull Null] Values in Nsuserdefaults, from JSON Serialization, Causes Unwanted Exceptions
Mask Uiview with Uibezierpath - Stroke Only
Having Uiview Drawrect Occur in a Background Thread
Convert Spelled Out Number to Number
Implementing Autocomplete in iOS
Custom Uitableviewcell with Progress Bar Download Update
iOS - Using Uisearchdisplaycontroller with Uisearchbar That Is Uibarbuttonitem in Uitoolbar
iOS Network Reachability - Doesn't Seem to Be Working
iOS Appextension:How to Combine Nsextensionactivationrule and Nspredicate
Why Is a Https Nsurlsession Connection Only Challenged Once Per Domain
Get Google Contacts Using API on iOS
Detecting iOS Version Number from User Agent Using Regular Expressions
How to Get Index(Of:) to Return Multiple Indices
How to Localize Label Strings in iOS for a Beginner