I am using phone number texfield, now i am using this format for texfield (#) ### ### ###, now issue is that i want first character 0 as compulsary, like this (0) 959 554 545, so user enter whatever first character must be typed 0,
this is my code
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newString = textField.text
if ((newString == "") && (newString?.count == 0)){
txtMobileNumber.text = "0"
return true
}else if ((newString?.count)! > 0){
return true
}
return false
}
If you create Enum, you can choice your textField type and make the extension properties for this field
In
shouldChangeCharactersIn
method ofUITextFieldDelegate
,Append
(0)
to the newly createdstring
, everytime thetextField
is edited.In
shouldChangeCharactersIn
method return false if new string count is greater than 15. Else remove(0)
and empty spaces, then if the string isn't empty add(0)
at the beginning. Then split the string by every 3 characters and join all string with space separator.