UiTextfield- First letter must be 0

后端 未结 3 1004
名媛妹妹
名媛妹妹 2021-01-17 04:03

I am using phone number texfield, now i am using this format for texfield (#) ### ### ###, now issue is that i want first character 0 as compulsary, like this (0) 959 554 54

3条回答
  •  梦谈多话
    2021-01-17 04:51

    If you create Enum, you can choice your textField type and make the extension properties for this field

    func textField(_ textField: UITextField,
                       shouldChangeCharactersIn range: NSRange,
                       replacementString string: String) -> Bool {
            if cellType == .phone {
                guard var sanitizedText = textField.text else { return true }
               if !sanitizedText.isEmpty && !sanitizedText.hasPrefix("(0)") {
                  sanitizedText.text = "(0)" + sanitizedText
               }
            }
    }
    

提交回复
热议问题