Dismiss Keyboard for textview - ios

I have been trying to get the keyboard during a textview to dismiss but it STILL doesn't change the return button's action.
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n"
{
textView.resignFirstResponder()
return false
}
return true
}
I was following the tutorial at https://www.youtube.com/watch?v=IsnoS8_G2SU
By now i have essentially copied the code 100%.
P.S. the textfield's outlet is named Textviews
Please help!
EDIT: Someone marked this as duplicate so let me explain why it isnt - I already have done what it tells me to do, and I'm not sure why it doesn't work for me.

You have made everything correct, but there is one step missing.
In order to hide or show the keyboard via the responder you have to set the delegate otherwise it won't work.
You can do it for example in the viewDidLoad:
override func viewDidLoad() {
myTextView.delegate = self
}

If your textfield is Textviews then you should have
Textviews.resignFirstResponder()
I hope this helps

Related

Resigning a text view as first responder and making a text field first responder when pressing return

I have an app with a text view and a text field where the user inputs a question in the view and an answer in the field. I want it to resign the view as first responder when the user presses return and make the field first responder and I tried the code below but it doesn't seem to be working and I can't figure out why
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text=="\n"{
questionTV.resignFirstResponder()
answerTF.becomeFirstResponder()
return false
}
return true
}
I've tried the following code:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
guard text == "\n" else { return true }
textView.resignFirstResponder()
textField.becomeFirstResponder()
return false
}
It works as expected.
I guess, the reason might be somewhere else. Do you have any more details to share?
Did you set text view's delegate? Do you stop at breakpoint inside shouldChangeTextIn method?
UPDATE:
In comments it came clear that you conformed to the wrong protocol. You need to conform to UITextViewDelegate instead of UITextFieldDelegate. You might want to keep UITextFieldDelegate conformance, if you need it for work with text field though.

Is there a way to hide a UILabel when the user starts typing, and then make it re-appear if the user removes all input?

I'm trying to implement a search field where you type in some characters to show "possible searches". In the beginning there is a small text underneath the textbox that says "make sure to capitalize letters". This is just a UILabel. I want to make this label "disappear" when the user STARTS to type. But if the user backspaces enough to remove all text - I want the label to re-appear again.
The textbox is just made from UITextField.
This is all stored in a view.
Does anyone have an idea of how to implement something like this?
You Can do it using add a target to your textFiled with a selector like this
add this target to ur viewDidLoad method
yourTextFiled.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
create selector method like this below your viewDidLoad Method and set if textfield.text.isEmpty than label is hidden like this
#objc func textFieldDidChange(_ textField: UITextField) {
if textField.text!.isEmpty {
label.isHidden = false
} else {
label.isHidden = true
}
}
Hope you get your desire thing !
You can do this by the textfield delegate method shouldChangeInCharacter and check when textfield is empty set the label isHidden property to false otherwise too true.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text {
if text.isEmpty {
label.isHidden = false
}
else {
label.isHidden = true
}
}
return true
}
Hope this will help you. Happy coding!

iOS, Swift, UITextField Changing Colour of Text being typed using typingAttributes, first letter's colour is not changed

I'm trying to change the colour of text being typed to white in UITextfield. I've used following code, which works but it doesn't change the colour of first letter to white. See the attached screenshot.
How to make it work so that colour of first letter also changes to white?
I've searched a lot but couldn't find the solution.
Tx in advance!
#IBAction func emailFieldEditingChanged(_ sender: UITextField) {
emailTextField.typingAttributes![NSAttributedStringKey.foregroundColor.rawValue] = UIColor.white
}
You'll want to set the delegate as Rakesha mentioned, but you have to change it each time the user inputs a character. Use this delegate method.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
emailTextField.typingAttributes![NSAttributedStringKey.foregroundColor.rawValue] = UIColor.white
return true
}
//Set this in viewDidLoad, and don't forget to include the delegate.
emailTextField.delegate = self

Swift: Disable or Clear Undo for a TextField

When selecting NumberPad for a TextField, the iPad displays a full keyboard, so I have created a function to simply remove all characters that are not numeric. However, if a Non-numeric key is pressed and then the user presses Undo on the keyboard, the App Crashes.
How do I disable the Undo function for the TextField or at least clear the Undo Stack?
The answer was simple in the end, I used undoManager removeAllActions, but instead of just needing:
undoManager?.removeAllActions()
as a line, I needed:
myTextField.undoManager?.removeAllActions()
I placed this in the DidChange Action for the TextField.
Add UITextFieldDelegate in your controller.
In your viewDidLoad() assign yourTextField.delegate = self. Than implement this method
public func textFieldShouldClear(textField: UITextField) -> Bool{
return false // This will disable clear button
}
Also for replacing text I would suggest use
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool{//Your Logic Here
}

Change 'Return' button function to 'Done' in swift in UITextView

I would like to get rid of the "return" function of the keyboard while the user is typing, so there are no new lines, so instead I would like the 'return' key to function as 'Done' so it would hide the keyboard.
I am using a UITextView, that is editable, so the user is able to type their post, and post it to the main timeline, but since I have fixed cells, I don't want the user to be able to press 'return' and their post would be out of range of the timeline.
I found this that works with UITextField, but not with UITextView:
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder() //if desired
return true
}
So I just wanted to know if there is a way to do that in a UITextView, or at least to be able to hide the keyboard if pressed return, instead of creating a new line.
You can set the return key type of the text field:
textField.returnKeyType = UIReturnKeyType.done
Update
You can definitely use the same approach to set the return key to "Done", as mentioned above. However, UITextView doesn't provide a callback when user hits the return key. As a workaround, you can try to handle the textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) delegate call, and dismiss the keyboard when you detect the input of a new line character:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") {
textView.resignFirstResponder()
}
return true
}
I have tried many codes and finally this worked for me in Swift 3.0 Latest [April 2019] this achieved using UITextFields
The "ViewController" class should be inherited the "UITextFieldDelegate" for making this code working.
class ViewController: UIViewController,UITextFieldDelegate
Add the Text field with the Proper Tag number and this tag number is used to take the control to appropriate text field based on incremental tag number assigned to it.
override func viewDidLoad() {
userNameTextField.delegate = self
userNameTextField.tag = 0
userNameTextField.returnKeyType = .next
passwordTextField.delegate = self
passwordTextField.tag = 1
passwordTextField.returnKeyType = .go
}
In the above code, the "returnKeyType = UIReturnKeyType.next" where will make the Key pad return key to display as "Next" you also have other options as "Join/Go" etc, based on your application change the values.
This "textFieldShouldReturn" is a method of UITextFieldDelegate controlled and here we have next field selection based on the Tag value incrementation.
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
nextField.becomeFirstResponder()
} else {
textField.resignFirstResponder()
return true;
}
return false
}
If you're working with a storyboard or xib, you can change the UITextView's Return button to 'Done' (or various other options) within Interface Builder, without the need for any setup code. Just look for this option in the Attributes inspector:
From there, you just pair it up with the UITextViewDelegate code that others have already provided here.
Swift v5:
extension ExampleViewController: UITextViewDelegate {
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") {
textView.resignFirstResponder()
}
return true
}
}
And then, in your viewDidLoad() method:
exampleTextView.delegate = self
Working in Swift 4
Add this in viewDidLoad().
textField.returnKeyType = UIReturnKeyType.Done
Add this anywhere you like.
extension UITextView: UITextViewDelegate {
public func textViewDidChange(_ textView: UITextView) {
if text.last == "\n" { //Check if last char is newline
text.removeLast() //Remove newline
textView.resignFirstResponder() //Dismiss keyboard
}
}
}

Resources