Format your input text with a CPF / CNPJ mask. You can add a custom mask too.
- Download and drop InputTextMask.swift in your project.
- Congratulations!
Clone this repository:
$ git clone https://github.com/falcaoaugustos/CPF_CNPJ-Mask.git
Open the CPF_CNPJ-Mask.xcodeproj file and build to see the example project.
Look at an example which editing a UITextField text attribute:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField.text.count == 14, string.count > 0 {
return false
}
textField.text = string.count < 1 ?
InputTextMask.applyMask(.CPF, toText: String(textField.text!.dropLast())) :
InputTextMask.applyMask(.CPF, toText: textField.text! + string)
return false
}
You can add a new pattern updating the MaskType enum:
public enum MaskType: String {
case CPF = "***.***.***-**"
case CNPJ = "**.***.***/****-**"
case birthday = "**/**/****"
}
Contributions are very welcome! If you'd like to contribute, feel free to send a pull request!
CPF_CNPJ-Mask is released under an MIT license. See LICENSE file for more information.