Skyscanner / SkyFloatingLabelTextField

A beautiful and flexible text field control implementation of "Float Label Pattern". Written in Swift.
Apache License 2.0
4.09k stars 540 forks source link

how to define maximum character length #309

Open cemalsayin opened 4 years ago

cemalsayin commented 4 years ago

Hi everyone, i'm creating payment view with credit card and i create SkyFloatingLabelTextField for card number you know card number text length 16. I've built card form with textFieldDidChange function (which puts "-" after every 4 character). I mean my SkyFloatingLabelTextField's max character should be 19. how can i define it?

ashokTEZSOL commented 4 years ago

you can use this method in "text field delegate"

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let maxLength = 19 let currentString: NSString = textField.text! as NSString let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString return newString.length <= maxLength }