Limiting the length to a limit on the number of characters in textfield causes UIlabel lengths to vary depending on the different language.
So I want to make it possible to type only as a fixed textfield width.
How to write limit a number of characters as textfield width? And when pasting text, how to cut off the text other than the textfield width?
This is my code
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if subtitleTextfield.text != nil {
nsString = subtitleTextfield.text! as NSString
nsString = nsString.replacingCharacters(in: range, with: string) as NSString
} else if subtitleTextfield.text != "" {
nsString = subtitleTextfield.text! as NSString
nsString = nsString.replacingCharacters(in: range, with: string) as NSString
} else if subtitleTextfield.text == "" {
subtitleTextfield.text = ""
}
guard let texts = subtitleTextfield.text else { return true }
let currentText = nsString as NSString
if currentText.length > 45 {
subtitleTextfield.text = currentText.substring(to: 45)
}
return currentText.length < 45
}