Skip to main content
Updated last answer to Swift 3 and taking advantage of the count and last methods for the text's characters
Source Link

Swift Code

Implement UITextViewDelegate in your class / View like so:

class MyClass: UITextViewDelegate { ... 

set the textView delegate to self

myTextView.delegate = self 

And then implement the following:

 func textViewDidChange(_ textView: UITextView) { if countElements(textView.text).characters.count >= 1 {  if let lastChar = textView.text.substringFromIndex(advance(textView.textcharacters.endIndex,last -1)){   if(lastChar == "\n"){     textView.text = textView.text.substringToIndexsubstring(advanceto: textView.text.index(before: textView.text.endIndex, -1))     textView.resignFirstResponder()   }  } } } 

EDIT I updated the code because it is never a good idea to change the user input in a textfield to for a workarround and not resetting the state after the hack code completed.

Swift Code

Implement UITextViewDelegate in your class / View like so:

class MyClass: UITextViewDelegate { ... 

set the textView delegate to self

myTextView.delegate = self 

And then implement the following:

func textViewDidChange(textView: UITextView){ if countElements(textView.text) >= 1 { let lastChar = textView.text.substringFromIndex(advance(textView.text.endIndex, -1)) if(lastChar == "\n"){ textView.text = textView.text.substringToIndex(advance(textView.text.endIndex, -1))   textView.resignFirstResponder() } } } 

EDIT I updated the code because it is never a good idea to change the user input in a textfield to for a workarround and not resetting the state after the hack code completed.

Swift Code

Implement UITextViewDelegate in your class / View like so:

class MyClass: UITextViewDelegate { ... 

set the textView delegate to self

myTextView.delegate = self 

And then implement the following:

 func textViewDidChange(_ textView: UITextView) { if textView.text.characters.count >= 1 {  if let lastChar = textView.text.characters.last {   if(lastChar == "\n"){     textView.text = textView.text.substring(to: textView.text.index(before: textView.text.endIndex))   textView.resignFirstResponder()   }  } } } 

EDIT I updated the code because it is never a good idea to change the user input in a textfield to for a workarround and not resetting the state after the hack code completed.

added 287 characters in body
Source Link

Swift Code

Implement UITextViewDelegate in your class / View like so:

class MyClass: UITextViewDelegate { ... 

set the textView delegate to self

myTextView.delegate = self 

And then implement the following:

func textViewDidChange(textView: UITextView){ if countElements(textView.text) >= 1 { let lastChar = textView.text.substringFromIndex(advance(textView.text.endIndex, -1)) if(lastChar == "\n"){ textView.text = textView.text.substringToIndex(advance(textView.text.endIndex, -1)) textView.resignFirstResponder() } } } 

EDIT I updated the code because it is never a good idea to change the user input in a textfield to for a workarround and not resetting the state after the hack code completed.

Swift Code

Implement UITextViewDelegate in your class / View like so:

class MyClass: UITextViewDelegate { ... 

set the textView delegate to self

myTextView.delegate = self 

And then implement the following:

func textViewDidChange(textView: UITextView){ if countElements(textView.text) >= 1 { let lastChar = textView.text.substringFromIndex(advance(textView.text.endIndex, -1)) if(lastChar == "\n"){ textView.resignFirstResponder() } } } 

Swift Code

Implement UITextViewDelegate in your class / View like so:

class MyClass: UITextViewDelegate { ... 

set the textView delegate to self

myTextView.delegate = self 

And then implement the following:

func textViewDidChange(textView: UITextView){ if countElements(textView.text) >= 1 { let lastChar = textView.text.substringFromIndex(advance(textView.text.endIndex, -1)) if(lastChar == "\n"){ textView.text = textView.text.substringToIndex(advance(textView.text.endIndex, -1)) textView.resignFirstResponder() } } } 

EDIT I updated the code because it is never a good idea to change the user input in a textfield to for a workarround and not resetting the state after the hack code completed.

Source Link

Swift Code

Implement UITextViewDelegate in your class / View like so:

class MyClass: UITextViewDelegate { ... 

set the textView delegate to self

myTextView.delegate = self 

And then implement the following:

func textViewDidChange(textView: UITextView){ if countElements(textView.text) >= 1 { let lastChar = textView.text.substringFromIndex(advance(textView.text.endIndex, -1)) if(lastChar == "\n"){ textView.resignFirstResponder() } } }