Thanks right now im a little bit closer but still there is issue when I try to delete more than 2 character and I got more than expected 2 letters close together. For example when I got text AB CD EF G and I try delete C the best thing for me would be AB DE FG without changing the cursor position. (I know my code looks terrible but I run out of ideas)
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
guard let currentText = textField.text,
let textRange = Range(range, in: currentText) else {
return true
}
let newText = currentText.replacingCharacters(in: textRange, with: string)
// if there is more than 4 spaces means we cant add extra " "
let whiteSpacesCount = newText.filter(\.isWhitespace).count
if newText.count > 13 {
return false
}
if !string.isEmpty {
if newText.count % 3 == 0 && whiteSpacesCount < 4 {
let insertionIndex = newText.index(newText.startIndex, offsetBy: newText.count - 1)
textField.text?.insert(" ", at: insertionIndex)
}
}
return true
}