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.
Related
On iOS13 I've noticed that after a textfield is cleared programatically the value for that textfield in the UI tests doesn't update.
In my UI test:
Text is entered into the textfield.
Some action happens which causes my code to clear the textfield (in this case tapping return on the on screen keyboard).
The value of the textfield is checked and it still contains the text that was present before it was cleared.
Here is the code I'm using to clear the textfield:
func clearInput() {
textField.text = nil
}
Here is the check I'm using in my UI test:
XCTAssert((createPasswordPage.passwordSecureTextField.value as? String) == "")
This fails on iOS13 but passes on older versions such as iOS12.
Any idea why this might be happening?
Example:
Code
class PasswordViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var button: UIButton!
...
#IBAction func buttonTapped(_ sender: Any) {
// Clear the text
textField.text = nil
}
}
UI Test
func testTextClearing() {
let page = PasswordPage()
// Enter text
page.passwordTextField.tap()
page.passwordTextField.typeText("some text")
// Tap button causing text to clear from the textfield
page.button.tap()
XCTAssert((page.passwordTextField.value as? String) == "")
}
When you set a UITextField's textContentType to .telephoneNumber, a suggested phone number will appear when you engage the text field.
When you tap the suggestion, the text field's text becomes the suggested phone number with the "+1" at the beginning. However, my text field has a drop down for users to select the country code so I don't want the "+1" to be included. Is there a way I can detect if the suggestion has been tapped and remove the "+1" from that text in the text field?
You can do something like this...
class ViewController: UIViewController {
#IBOutlet weak var yourTextfield: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
yourTextfield.addTarget(self, action: #selector(textFiedDidChange(_:)), for: .editingChanged)
}
#objc func textFiedDidChange(_ sender: Any) {
let prefix = "+1" // What ever you want may be an array and step thru it
guard yourTextfield.text!.hasPrefix(prefix) else { return }
yourTextfield.text = String(yourTextfield.text!.dropFirst(prefix.count).trimmingCharacters(in: .whitespacesAndNewlines))
}
}
This is a very basic question but I'm not sure what the problem is. I'm trying to make a simple "hello world" program where the user inputs what they want into the textfield and whatever they enter goes into the label. However, nothing seems to be happening and I'm unsure why since my push function worked exactly how I expected it to.
import UIKit
class ViewController: UIViewController {
#IBOutlet var PopUp: UITextField!
#IBOutlet weak var HelloWorld: UILabel!
#IBAction func Push(_ sender: UIButton) {
PopUp.isHidden = false
PopUp.text = "hello World"
}
#IBAction func send(_ sender: UITextField) {
HelloWorld.text = sender.text
}
}
Based on the code you provided, func send is an unknown to me, as to whether or not it is even firing. func send might be called, might not, either way, it is strange to see a _ sender: UITextField for an IBAction.
What event are you firing related to the UITextField? Are you trying to update your HelloWorld UILabel as the user types in the UITextfield?
To update your UILabel with whatever has been typed in your UITextField, you just need a UIButton Touch Up Inside IBAction. I think you can delete IBAction func send completely, unless you are trying to update the UILabel as the user types in the UITextField. Make sure you remove the IBAction Outlet from your Storyboard if I am correct about this point.
Based on the code you provided the Push func does not set text in your UILabel. I am assuming Push is an UIButton Touch Up Inside IBAction. You can set your HelloWorld UILabel text, in func Push, you do not need to use the sender of the event, try this and you will see the HelloWorld UILabel text populated:
#IBAction func Push(_ sender: UIButton) {
//PopUp.isHidden = false //why are you doing this? the UITextField PopUp should already be visible if you are typing text into it, so this code is superfluous as the value of PopUp.isHidden is already false
HelloWorld.text = PopUp.text
PopUp.text = "hello World"
}
If you are trying to have your UILabel display the text as you type into the UITextField you should clarify your question. And if it is the case you will need to make your UIViewController a UITextFieldDelegate
Here is the one way of approach :
override func viewDidLoad() {
super.viewDidLoad()
PopUp.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged) // when textfield is edited this will call
}
func textFieldDidChange(_ textField: UITextField) {
HelloWorld.text = textField.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 am creating a post to twitter app and want to add various text to the UITextField from buttons that can be selected. The UIButton are for various hashtags to speed the process of writing a tweet. I have tried a couple the solutions found on Stack but none seem to work and they are all in Objective-C.
Screenshot of compose tweet view before buttons touched.
http://postimg.org/image/5qoyk6673/
Screenshot of compose tweet view after button selected and text added to text field.
http://postimg.org/image/vp08wsa6b/fa7c7a83/
class TweetComposeViewControler: UIViewController, UITextViewDelegate {
var selectedAccount : ACAccount!
#IBOutlet var tweetContent: UITextView!
#IBAction func specializedButton(sender: UIButton) {
tweetContent.text = sender as UIButton
tweetContent.text.stringByAppendingString(specializedButton(titleLabel.text))
}
func insertHashtag(sender: UIButton!) {
tweetContent.text = tweetContent.text.stringByAppendingString(sender.titleLabel.text)
}
You can set the UITextFields text to the title of your UIButton:
txtField.text = hashTagButton.titleLabel.text
And if you want to append the text:
txtField.text = textField.text.stringByAppendingString(hashTagButton.titleLabel.text)
In order to have your buttons update the text upon pressing them you need to add a target selector for when the buttons are pressed. This can be done via Storyboard or programmatically.
Programmatically
You would add the same selector to all buttons using this:
hashtagButton.addTarget(self, action: Selector("insertHashtag:"), forControlEvents: .TouchUpInside)
This will call the insertHashtag function whenever the hashtagButton is pressed. Because of the : at the end of the selector, it will also pass itself as a parameter so you can use it to get the button's title rather than creating a different selector for each button.
func insertHashtag(sender: UIButton!) {
txtField.text = textField.text.stringByAppendingString(sender.titleLabel!.text)
}
Using IBAction
#IBAction func insertHashtag(sender: AnyObject) {
txtField.text = sender as UIButton
textField.text.stringByAppendingString(btn.titleLabel!.text)
}
Here you cast the sender paramter as a UIButton since you know a UIButton is the type of the object which called it.
If you know that only UIButton's will cause this method you can do this:
#IBAction func insertHashtag(btn: UIButton ) {
txtField.text = sender as UIButton
textField.text.stringByAppendingString(btn.titleLabel!.text)
}
Response to your update
The code you added needs to be fixed to look like this:
#IBAction func specializedButton(sender: UIButton) {
tweetContent.text.stringByAppendingString(sender.titleLabel!.text)
}
Here sender is your UIButton and you pass it as a UIButton so you do not need to convert it.