Skip to main content
added 51 characters in body
Source Link
Dan Beaulieu
  • 20k
  • 19
  • 107
  • 138

Add UITextViewDelegate to your class and then set your delegate for your textView or your textField in viewDidLoad. Should look something like this:

myTextField// in viewDidLoad textField.delegate = self textView.delegate = self 

Swift 3

// hides text views func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if (text == "\n") { textView.resignFirstResponder() return false } return true } // hides text fields func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if (string == "\n") { textField.resignFirstResponder() return false } return true } 

Swift 2.0

The below syntax has been tested for Swift 1.2 & Swift 2.0

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if(text == "\n") { textView.resignFirstResponder() return false } return true } 

Note: at one point this answer was unique from the accepted answer

Add UITextViewDelegate to your class and then set your delegate for your textView or your textField in viewDidLoad. Should look something like this:

myTextField.delegate = self 

Swift 3

// hides text views func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if (text == "\n") { textView.resignFirstResponder() return false } return true } // hides text fields func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if (string == "\n") { textField.resignFirstResponder() return false } return true } 

Swift 2.0

The below syntax has been tested for Swift 1.2 & Swift 2.0

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if(text == "\n") { textView.resignFirstResponder() return false } return true } 

Note: at one point this answer was unique from the accepted answer

Add UITextViewDelegate to your class and then set your delegate for your textView or your textField in viewDidLoad. Should look something like this:

// in viewDidLoad textField.delegate = self textView.delegate = self 

Swift 3

// hides text views func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if (text == "\n") { textView.resignFirstResponder() return false } return true } // hides text fields func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if (string == "\n") { textField.resignFirstResponder() return false } return true } 

Swift 2.0

The below syntax has been tested for Swift 1.2 & Swift 2.0

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if(text == "\n") { textView.resignFirstResponder() return false } return true } 
added 606 characters in body
Source Link
Dan Beaulieu
  • 20k
  • 19
  • 107
  • 138

Add UITextViewDelegate to your class and then set your delegate for your textView or your textField in viewDidLoad. Should look something like this:

myTextField.delegate = self 

Swift 3

// hides text views func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if (text == "\n") { textView.resignFirstResponder() return false } return true } // hides text fields func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if (string == "\n") { textField.resignFirstResponder() return false } return true } 

Swift 2.0

The below syntax has been tested for Swift 1.2 & Swift 2.0

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if(text == "\n") { textView.resignFirstResponder() return false } return true } 

Note: at one point this answer was unique from the accepted answer

Swift 2.0

The below syntax has been tested for Swift 1.2 & Swift 2.0

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if(text == "\n") { textView.resignFirstResponder() return false } return true } 

Note: at one point this answer was unique from the accepted answer

Add UITextViewDelegate to your class and then set your delegate for your textView or your textField in viewDidLoad. Should look something like this:

myTextField.delegate = self 

Swift 3

// hides text views func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if (text == "\n") { textView.resignFirstResponder() return false } return true } // hides text fields func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if (string == "\n") { textField.resignFirstResponder() return false } return true } 

Swift 2.0

The below syntax has been tested for Swift 1.2 & Swift 2.0

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if(text == "\n") { textView.resignFirstResponder() return false } return true } 

Note: at one point this answer was unique from the accepted answer

edited body
Source Link
swiftBoy
  • 35.8k
  • 26
  • 140
  • 137

Swift 2.0

The below syntax has been tested for Swift 1.2 & Swift 2.0

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if(text == "\n") { textView.resignFirstResponder() return false } return true } 

noteNote: at one point this answer was unique from the accepted answer

Swift 2.0

The below syntax has been tested for Swift 1.2 & Swift 2.0

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if(text == "\n") { textView.resignFirstResponder() return false } return true } 

note: at one point this answer was unique from the accepted answer

Swift 2.0

The below syntax has been tested for Swift 1.2 & Swift 2.0

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if(text == "\n") { textView.resignFirstResponder() return false } return true } 

Note: at one point this answer was unique from the accepted answer

added 66 characters in body
Source Link
Dan Beaulieu
  • 20k
  • 19
  • 107
  • 138
Loading
deleted 26 characters in body
Source Link
Dan Beaulieu
  • 20k
  • 19
  • 107
  • 138
Loading
Source Link
Dan Beaulieu
  • 20k
  • 19
  • 107
  • 138
Loading