I have a textView and i need to update my textView content and save the edited text. In my case everything is working but in the UI edited text is updated.self.taskName data get it from the previous ViewController
In viewDidLoad i was written like this
self.taskNameTextView.text = self.taskName
self.taskNameTextView.delegate = self
In textViewDelegate,
func textViewDidChange(_ textView: UITextView) {
self.taskName = textView.text
}
and button action i have to passed the edited textString and it return successful but not changed updated text in UI.Can you please anyone help me to figure out this problem.
Rather than textViewDidChange please write your code inside
func textViewDidEndEditing(_ textView: UITextView) {
// your code here
}
For more delegate functions of TextView please refer to this post;
How to detect text view begin editing and end editing in swift 3
Related
I want to call an animation when my UITextView is empty and again when it is not empty.
I get the UITextView changes in the delegate method below;
func textViewDidChange(_ textView: UITextView) {
}
However, this checks if the UITextView is empty every time it changes. I only want to call this once.
Is there any method for this?
This delegate is handled by UIKit, therefore is not possible to select when it is called. However, there are these other options according to Apple docs:
func textViewDidBeginEditing(_ textView: UITextView)
func textViewDidEndEditing(_ textView: UITextView)
This question already has an answer here:
iOS: Programmatically move cursor up, down, left and right in UITextView
(1 answer)
Closed 3 years ago.
Can someone show me how to move the textView cursor downward cause I don't know how to do it,
Also where should I put it
in the textViewDidBeginEditing(_ textView: UITextView) or in the textViewDidEndEditing(_ textView: UITextView)
Thanks in advance
This will move coursor to the end of text:
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
DispatchQueue.main.async {
textView.selectedRange = NSRange(location: textView.text.count, length: 0)
}
return true
}
If you want to begin text from a new line, add this, before return true:
textView.text = textView.text + "\n"
If you want to skip two lines, make it "\n\n", and so on.
I have a UITextView and a UIButton in my app and I'm trying to get the text content of the UITextView to be cleared when the UIButton is tapped.
My code:
#IBOutlet weak var textView: UITextView!
#IBAction func ClearButtonTapped(_ sender: UIButton) {
// I want to clear the text content of textView
}
Is there built-in function for that, in the UITextView class? I didn't find anything when I searched the UITextView class in Xcode.
My app is on Xcode 10.1 and Swift 4.2.
Small improvement:
textView.text = nil
Try using textView.text = "". If that's not working it could be that you're using a placeholder. Try textView.placeholder = ""
I didn't find a ready function to clear the text content of an UITextView so I created this code to do that:
The UITextView variable:
#IBOutlet weak var textView: UITextView!
Function to clear the UITextView:
#IBAction func ClearButtonTapped(_ sender: UIButton) {
textView.selectAll(textView)
if let range = textView.selectedTextRange { textView.replace(range, withText: "") }
}
When the clear-button is tapped, the function checks is there any text in the UITextView and if there is some, it will select all the text in the UITextView and replace it with an empty String.
EDIT:
There is also the simple way to do it, which, for some reason, didn't work when I tried it before (probably because the bugs in Xcode 10.1), but this way the user can't undo it, if they accidentally tap the clear button:
#IBAction func ClearButtonTapped(_ sender: UIButton) {
textView.text = ""
}
Or with extension:
extension UITextView {
func clear() {
self.text = ""
}
}
Call textView.clear() when you want to clear the text.
I am trying to clear multiple textviews on editing. I know how do so with one textView (IE):
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var myTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
myTextView.delegate = self
}
func textViewDidBeginEditing(textView: UITextView) {
myTextView.text = ""
}
How would I use the same concept for multiple textviews?
func textViewDidBeginEditing(textView: UITextView) {
myTextView.text = ""
}
The above delegate method will get called when a text view begins editing. And this method holds the reference to the textView that called in the textView object. You can use that reference to clear the text instead of using a separate reference/outlet to the textView.
So the method would be:
func textViewDidBeginEditing(textView: UITextView) {
textView.text = ""
}
From the Documentation:
Description:
Tells the delegate that editing began in the specified text field.
This method notifies the delegate that the specified text field just
became the first responder. Use this method to update state
information or perform other tasks. For example, you might use this
method to show overlay views that are visible only while editing.
Implementation of this method by the delegate is optional.
Parameters:
textView
The text view in which an editing session began.
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