Giter VIP home page Giter VIP logo

Comments (4)

filipealva avatar filipealva commented on July 21, 2024 1

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

from akmaskfield.

kvacquier avatar kvacquier commented on July 21, 2024

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

from akmaskfield.

andreikeda avatar andreikeda commented on July 21, 2024

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

from akmaskfield.

BruhhGarcia avatar BruhhGarcia commented on July 21, 2024

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?

from akmaskfield.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.