this question has answer here:
i add "-" between 4 numbers when user typing in pinno text field.
example, 1234 - 5678 - 9932
// try this
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 }