Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable size fields #28

Open
filipealva opened this issue Jan 3, 2017 · 4 comments
Open

Variable size fields #28

filipealva opened this issue Jan 3, 2017 · 4 comments

Comments

@filipealva
Copy link

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!

@kvacquier
Copy link

I would like to use it for an IP Address and i have the same exact question

@lazymug
Copy link

lazymug commented Nov 15, 2017

@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`

@BruhhGarcia
Copy link

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?

@filipealva
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants