Open filipealva opened 7 years ago
I would like to use it for an IP Address and i have the same exact question
@filipealva I don't know if I understand your question, but you can make something like this:
`let EXPRESSION: String! = "{ddddd}-{d}" let TEMPLATE: String! = "ddddd-d"
yourTextField.maskTemplate = TEMPLATE yourTextField.maskExpression = EXPRESSION`
Hi, I have the same question. In my case I have a phone number that could be ({dd}){dddd}-{dddd} or ({dd}){ddddd}-{dddd} Is there a way to use two different expression in the same text field?
@andreikeda the problem is that if I use {ddddd}-{d}
the -
will only be placed when the user inputs 6 characters. I needed it to be placed before the last character even if there is only 4 characters.
@BruhhGarcia @kvacquier I wind up implementing it by myself using the UITextFieldDelegate
, here goes the code that fit my needs:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//Getting the textfield text
var text: NSString = textField.text! as NSString
text = text.replacingCharacters(in: range, with: string) as NSString
//Adding the textfield text on a mutable string
var mutableString: NSMutableString = text.mutableCopy() as! NSMutableString
//Verifying if the user not tapped the delete key
if(!(range.length == 1 && string.lengthOfBytes(using: String.Encoding.utf8) == 0)) {
//Adding the formatter character at the correct position
if (mutableString.length > 1) {
mutableString = (mutableString.replacingOccurrences(of: "-", with: "") as NSString).mutableCopy() as! NSMutableString
mutableString.insert("-", at: mutableString.length - 1)
}
}
let char = string.cString(using: String.Encoding.utf8)!
let isBackSpace = strcmp(char, "\\b")
if (isBackSpace == -92 && mutableString.length > 1) {
mutableString = (mutableString.replacingCharacters(in: NSMakeRange(mutableString.length - 1, 1), with: "") as NSString).mutableCopy() as! NSMutableString
if (mutableString.length > 1) {
mutableString = (mutableString.replacingOccurrences(of: "-", with: "") as NSString).mutableCopy() as! NSMutableString
mutableString.insert("-", at: mutableString.length - 1)
}
}
//Putting the string with the formatter character on the textfield
textField.text = mutableString as String
return false
}
Maybe with some adjustments you will be able to adapt it to your needs.
I have a field which support two number sequences separated by a
-
char.The first sequence can hold 4 or 5 numbers, and the second one can only have one number (0-9).
There is some way to make this mask with
AKMaskField
?Thanks in advance!