Adding space between textfield character when typing in text filed

前端 未结 3 886
太阳男子
太阳男子 2020-12-17 07:14

I have a textfield with maximum character range 16, After every 4 characters, I want to add minus character or space and then writing The rest of the characters

3条回答
  •  情书的邮戳
    2020-12-17 07:30

    To do it "on the fly", I connected Editing changed to this IBAction ; needs also to care for backspace

     @IBAction func editingTestField(_ sender: UITextField) {
          if sender.text!.count > 0 && sender.text!.count % 5 == 0 && sender.text!.last! != "-" {
             sender.text!.insert("-", at:sender.text!.index(sender.text!.startIndex, offsetBy: sender.text!.count-1) )
          }
     }
    

提交回复
热议问题