Brazilian phone mask

1

I'm migrating from Android to iOS now, and I have a question. Can I make Swift something like onTextChanged of Java?

I need a mask for my UITextField which is dynamic, according to the number of characters entered ( ## ####-#### or ## #####-#### ).

How can I handle this in Swift 3?

    
asked by anonymous 01.11.2017 / 13:56

2 answers

0

You can use the textField function shouldChangeCharactersIn range. To do this, you need to connect the UITextField delegate to your ViewController (either through the storyboard or through code), create an outlet for this textField in your ViewController code, implement the UITextFieldDelegate protocol, and use the code as described below. It will allow you to format for the Brazilian number already including the fifth number after the area code. To make it clear, below where I use "cellPhoneTextField", you should use the textField's outlet name for the phone number you have connected to the storyboard.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if textField == cellPhoneTextField {

        //New String and components
        let newStr = (textField.text! as NSString).replacingCharacters(in: range, with: string)
        let components = (newStr as NSString).components(separatedBy: NSCharacterSet.decimalDigits.inverted)

        //Decimal string, length and leading
        let decimalString = components.joined(separator: "") as NSString
        let length = decimalString.length
        let hasLeadingOne = length > 0 && decimalString.character(at: 0) == (1 as unichar)

        //Checking the length
        if length == 0 || (length > 11 && !hasLeadingOne) || length > 13 {
            let newLength = (textField.text! as NSString).length + (string as NSString).length - range.length as Int

            return (newLength > 11) ? false : true
        }

        //Index and formatted string
        var index = 0 as Int
        let formattedString = NSMutableString()

        //Check if it has leading
        if hasLeadingOne {
            formattedString.append("1 ")
            index += 1
        }

        //Area Code
        if (length - index) > 2 {
            let areaCode = decimalString.substring(with: NSMakeRange(index, 2))
            formattedString.appendFormat("%@ ", areaCode)
            index += 2
        }

        if length - index > 5 {
            let prefix = decimalString.substring(with: NSMakeRange(index, 5))
            formattedString.appendFormat("%@-", prefix)
            index += 5
        }

        let remainder = decimalString.substring(from: index)
        formattedString.append(remainder)
        textField.text = formattedString as String
        return false

    } else {
        return true
    }

}
    
01.11.2017 / 16:32
0

For Swift development, the best thing to do to save time and lines of code is to use Frameworks , an extremely abundant thing in iOS .

In this case I suggest using TLCustomMask , which is easy to implement using PodFile.

Follow the GitHub framework TLCustomMask

    
11.12.2018 / 14:29