Created
March 24, 2022 08:05
-
-
Save DisappearPing/b677eb59a1ac98e92f0cc90884963b37 to your computer and use it in GitHub Desktop.
TextField limit enter string length
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
// TextField | |
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { | |
guard let textFieldText = textField.text, | |
let rangeOfTextToReplace = Range(range, in: textFieldText) else { | |
return false | |
} | |
let substringToReplace = textFieldText[rangeOfTextToReplace] | |
let count = textFieldText.count - substringToReplace.count + string.count | |
return count <= 10 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
limit for 10 digit enter for example usage