Clear the content of UITextView in Swift

textfieldshouldclear swift
uitextfield clear button action
textfieldshouldclear not called
uitextfield clear button not showing
ios clear text field programmatically swift
uitextfield detect clear button pressed
custom clear button uitextfield swift
uitextfield clear button color

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

How to clear UITextView upon editing?, Asks the delegate if the text field's current contents should be removed. Availability. iOS 2.0+; Mac Catalyst 13.0+; tvOS 9.0+. Framework Declaration. optional func textFieldShouldClear(_ textField: UITextField) -> Bool The text field calls this method in response to the user pressing the built-in clear button. ( This button is� 2) place UITextView on top of this UIView. Place it so that the borders of the underlying UIView stay visible. 3) give the UITextView round corners: self.textNote.layer.cornerRadius = 9.0f; 3) Make its width fe. 30pixels less compared to the underlying UIView. You now have space for a clear-button.

Try using textView.text = "". If that's not working it could be that you're using a placeholder. Try textView.placeholder = ""

textFieldShouldClear(_:), UITextView - Paste action inserts empty string on iOS 11. I have a regular ` UITextView` that has Paste support out of the box via `UIKit`. Nothing fancy in the � Overview. UIText View supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document.

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.

UITextView, Hey, aren't you the guy that also publishes courses on Linkedin? I very much enjoyed your course on Swift protocol oriented programming. Clear Button. The behavior of the clear button. The clear button is a built-in overlay view that the user taps to delete all of the text in a text field. Use this attribute to define when the clear button appears. To set this attribute programatically, use theclear Button Mode property. Min Font Size. The minimum font size for the text field

Here's how to add a clear button to the UITextView (was: The Tale of , If you want to let users clear their entry on a UITextField , the standard approach is to add a clear button to the right edge of the text field. We have introduced how to use UITextField and UITextView in article iOS UITextField And UITextView Swift Example.In that article, we use interface builder and storyboard to create the user interface, assign delegate protocol to the view controller, implement delegate methods to response user action ( for example : begin editing text field, etc.).

How to make a clear button appear in a textfield, Learn Swift coding for iOS with these free tutorials. If you have a UITextField or UITextView and want to stop users typing in more than a the current text, or use an empty string if that failed let currentText = textField.text ? Extending TextKit. To trim text we need first find the range of text that fits to maximumNumberOfLines.To do so we will use TextKit. UITextView already uses it to render it’s text on screen and has built in components like text container, text storage and layout manager.

How to limit the number of characters in a UITextField or UITextView , This allows UITextView to have an intrinsic content size depending on the text, or close to the text In the TableTableViewController.swift -> cellForRow So our next move was to remove that padding from the UITextView. In this tutorial you are going to learn how to use UITextView how to deal with the iOS onscreen keyboard and more. 🎄 All UIKit Advent Calendar Videos 🎄 https

Comments
  • Why downvotes? What’s wrong with the question?
  • Yeah, not sure why the down votes. As an extra check, can we assume you've connected your outlets correctly in Interface Builder? The other answers given by others below are good suggestions to clearing text. By the way, I believe your UITextView outlet should be strongly referenced not weakly referenced according to: stackoverflow.com/questions/7678469/…
  • @Zhang Why I should use Strong rather than Weak? There was a lot of opinions in the link you posted and Xcode uses Weak for default.
  • Well, numerous people have cited Apple engineers recommending to use strong, one even mentioned performance benefits. As long as your app is working, whatever floats the boat I guess. I myself like to think of buttons defined inside a UIViewController as belonging to that controller, forming a single self contained coherent unit, so I maintain a strong reference to it. The thought of something weakly attached to my UIViewController feels like it's dangling on, gives me an uncomfortable feeling metaphorically speaking.
  • @Zhang Ok, thanks for the clarification.
  • Why not textView.text = "" ?
  • Do you mean to clear a portion/selected or all the content ?
  • All the content
  • This only @IBAction func ClearButtonTapped(_ sender: UIButton) { textView.text = "" } doesn't work ?
  • Now it works. Strange, because it didn't do anything when I tried it before. Maybe the problem was in Xcode; when I have renamed some variables and functions before, it haven't always change the names in everywhere in my project.