Last active
November 2, 2023 02:36
-
-
Save kothihaaung/4b99eb293b702d1c965692d7af20d5c5 to your computer and use it in GitHub Desktop.
Convert to uppercase even when pasted text in the textfield.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
public class AutoUpperCasingTextField: UITextField, UITextFieldDelegate { | |
// Max char to limit. | |
var maxNumber: Int = 0 { | |
didSet { | |
self.delegate = self | |
} | |
} | |
// Flag to convert every input text to Uppercase | |
var autoUppercasing = false | |
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { | |
guard let textFieldText = self.text, | |
let rangeOfTextToReplace = Range(range, in: textFieldText) else { | |
return false | |
} | |
// Char limitation process | |
let substringToReplace = textFieldText[rangeOfTextToReplace] | |
let count = textFieldText.count - substringToReplace.count + string.count | |
guard count <= self.maxNumber else { | |
return false | |
} | |
// Stop right there for limitation only. | |
if autoUppercasing == false { | |
return true | |
} | |
// Uppercasing process | |
let lowercaseCharacters = NSCharacterSet.lowercaseLetters | |
if string.rangeOfCharacter(from: lowercaseCharacters) != nil { | |
let uppercaseString = string.uppercased() | |
guard let inputText = textField.text else { | |
return false | |
} | |
if inputText.isEmpty { | |
// Updates return button; forces cursor to the end | |
textField.text = (inputText as NSString).replacingCharacters(in: range, with: uppercaseString) | |
} else { | |
// Preserves cursor location; doesn't update return button | |
let beginning = textField.beginningOfDocument | |
guard let start = textField.position(from: beginning, offset: range.location) else { | |
return false | |
} | |
guard let end = textField.position(from: start, offset: range.length) else { | |
return false | |
} | |
guard let range = textField.textRange(from: start, to: end) else { | |
return false | |
} | |
textField.replace(range, withText: uppercaseString) | |
} | |
return false | |
} else { | |
return true | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Prevent paste action in UITextField. | |
class PaymentInfoExpireTextField: UITextField { | |
// Diable paste function in credit card expire date textfields. | |
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { | |
if action == #selector(UIResponderStandardEditActions.paste(_:)) { | |
return false | |
} | |
return super.canPerformAction(action, withSender: sender) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swift