答案 0 :(得分:6)
//试试这个
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
var strText: String? = textField.text
if strText == nil {
strText = ""
}
strText = strText?.stringByReplacingOccurrencesOfString("-", withString:"")
if strText!.characters.count > 1 && strText!.characters.count % 4 == 0 && string != "" {
textField.text = "\(textField.text!)-\(string)"
return false
}
return true
}