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.
Disable cursor and copy/paste in UITextView (swift)
I think the easy way is to make it with a button instead UITextField
Sorry - this is not for Swift
it's for Obj-C
but this is the idea:
To do what you want with UITextField
you have to subclass the UITextField
and try to use this code to disable/hide caret and input (copy/paste)
- (CGRect) caretRectForPosition:(UITextPosition*) position
{
return CGRectZero;
}
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return nil;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(copy:) || action == @selector(selectAll:) || action == @selector(paste:))
{
returnNO;
}
return [super canPerformAction:action withSender:sender];
}
example from here: http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/
Anyway... this is a "functional" example: https://github.com/hackiftekhar/IQDropDownTextField
How control Copy, Paste, Select All, Define in UITextView iPhone app?
First of all if the code you have above isn't working then you probably forgot to change the class of you UITextView to your custom class that implements the method above.
Once you've done that what you've got should work and you should then return no for select all also
if (action == @selector(selectAll:))
{
return NO;
}
also you may want to return no for cut: also assuming you don't want the user to remove text from the textView.
Also these don't need to be if else statements as they don't depend on each other
They are actually called in this order
cut:
copy:
select:
selectAll:
paste:
delete:
So remove functionality as appropriate.
Related Topics
Iphone 6 Plus Resolution Confusion: Xcode or Apple'S Website? For Development
Setting Action For Back Button in Navigation Controller
Objective-C and Swift Url Encoding
How to Add Uitableview Within a Uitableviewcell
Ios 7: Uitableview Shows Under Status Bar
How to Find Out the Type of an Object (In Swift)
How to Convert a Uiview to an Image
Convert HTML to Plain Text in Swift
Returning Method Object from Inside Block
What Is "Constrain to Margin" in Storyboard in Xcode 6
Uilabel Sizetofit Doesn't Work With Autolayout Ios6
How to Detect First Time App Launch on an Iphone
How to Change the Background Color of a Uibutton While It's Highlighted