Formatting a UITextField for credit card input like (xxxx xxxx xxxx xxxx)

前端 未结 28 2175
长情又很酷
长情又很酷 2020-11-28 01:19

I want to format a UITextField for entering a credit card number into such that it only allows digits to be entered and automatically inserts spaces so that the

28条回答
  •  伪装坚强ぢ
    2020-11-28 01:49

    Please use simple form of credite card /** See sample usage: ### let str = "41111111111111111"

     let x = yourClassname.setStringAsCardNumberWithSartNumber(4, withString: str!, withStrLenght: 8)
    
     ### output:- 4111XXXXXXXX1111
    
     let x = yourClassname.setStringAsCardNumberWithSartNumber(0, withString: str!, withStrLenght: 12)
    
     ### output: - XXXXXXXXXXXX1111
    
     */
    func setStringAsCardNumberWithSartNumber(Number:Int,withString str:String ,withStrLenght len:Int ) -> String{
        //let aString: String = "41111111111111111"
        let arr = str.characters
        var CrediteCard : String = ""
        if arr.count > (Number + len) {
            for (index, element ) in arr.enumerate(){
                if index >= Number && index < (Number + len) {
                    CrediteCard = CrediteCard + String("X")
                }else{
                    CrediteCard = CrediteCard + String(element)
                }
            }
          return CrediteCard
        }else{
                print("\(Number) plus \(len) are grether than strings chatarter \(arr.count)")
        }
        print("\(CrediteCard)")
        return str
    }
    

    I hope this is helpful to you.

提交回复
热议问题