Is There a Preferred Technique to Prohibit Pasting into a Uitextfield

Is there a preferred technique to prohibit pasting into a UITextField?

You can not override a class method using an extension.

from the docs "NOTE Extensions can add new functionality to a type, but they cannot override existing functionality."

What you need is to subclass UITextField and override your methods there:

To only disable paste functionality:

class TextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste) {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}

Usage:

let textField = TextField(frame: CGRect(x: 50, y: 120, width: 200, height: 50))
textField.borderStyle = .roundedRect
view.addSubview(textField)

To allow only copy and cut:

class TextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
[#selector(UIResponderStandardEditActions.cut),
#selector(UIResponderStandardEditActions.copy)].contains(action)
}
}

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.

Sample Image

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.

Preventing pasting into a textField in swift 5

This code will make the textfield to accept only numbers.

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let invalidCharacters = CharacterSet(charactersIn: "0123456789").inverted
return string.rangeOfCharacter(from: invalidCharacters) == nil
}

And This code will prevent paste. This enables only cut and copy functions.

class PastelessTextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return super.canPerformAction(action, withSender: sender)
&& (action == #selector(UIResponderStandardEditActions.cut)
|| action == #selector(UIResponderStandardEditActions.copy))
}
}

How to remove the iPad's bottom copy/paste bar when editing a UITextView?

You can hide this as below,

yourTextView.inputAssistantItem.leadingBarButtonGroups = []
yourTextView.inputAssistantItem.trailingBarButtonGroups = []

Is there a way to disable predictive text off for the entire ios app like UIAppearance protocol?


  1. You can do it by assigning the property autocorrectionType to false in every textfield that you have.
  2. You can subclass UITextField class and use it in every view controller that you want.
  3. You can use Method Swizzling in order to auto set this property for UITextField class

Hide the cursor of a UITextField

Simply subclass UITextField and override caretRectForPosition

- (CGRect)caretRectForPosition:(UITextPosition *)position
{
return CGRectZero;
}

UITextField - Prevent user from typing non-standard characters (like Emoticons)

You can combine NSCharacterSets using a union to add more allowable characters:

    NSMutableCharacterSet *myCharSet = [[NSCharacterSet alphanumericCharacterSet] mutableCopy];
[myCharSet formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet];

Here is the list of available character sets to choose from:

controlCharacterSet
whitespaceCharacterSet
whitespaceAndNewlineCharacterSet
decimalDigitCharacterSet
letterCharacterSet
lowercaseLetterCharacterSet
uppercaseLetterCharacterSet
nonBaseCharacterSet
alphanumericCharacterSet
decomposableCharacterSet
illegalCharacterSet
punctuationCharacterSet
capitalizedLetterCharacterSet
symbolCharacterSet
newlineCharacterSet

Also, your code is checking to make sure any character is not outside the set; I think your intention is to check that all characters are not outside the set. (This only applies when the user enters more than one character at once - think copy and paste).

Consider adjusting your loop to look more like this:

NSCharacterSet *myCharSet = [NSCharacterSet alphanumericCharacterSet];
for (int i = 0; i < [string length]; i++) {
unichar c = [string characterAtIndex:i];
if ([myCharSet characterIsMember:c] == NO) {
// add your user alert here
return NO;
}
}

This way your loop won't simply exit on the first good character it finds; instead, it will exit on the first bad character.



Related Topics



Leave a reply



Submit