In the below textView function, I append the '#' character to the end of my string every time the user presses the spacebar. However, an unwanted space comes after the appended character. I've used functions to remove whitespaces at the end of the string but those don't help. Furthermore, I also tried to remove the last character of my string using removeLast() but that just removes the '#' character itself. Please help! I'm using swift 4.2.
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == " " {
textView.text.append(("#"))
//textView.text.removeLast()
}
if text == "\n" {
textView.resignFirstResponder()
return false
}
return true
}