Im Batman
Im Batman

Reputation: 1876

How can I edit a UILabel upon touching it in Swift?

I want to know how to edit the label of text while the app is running.

Example: there will be label called "Tap to Change text." When the user clicks it, it will be editable and the user can input text and enter. Then it will change to new text.

I know this can be done using UITextFieldDelegate,but I don't know how to approach to it because there is no way to put an action to a label when user touches it.

Upvotes: 11

Views: 15695

Answers (5)

Janet Wang
Janet Wang

Reputation: 1

I made some adjustments on the selector from Dharmesh's answer. Hope this helps.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var lbl: UILabel!
    @IBOutlet weak var textF: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        textF.delegate = self
        textF.hidden = true
        lbl.userInteractionEnabled = true
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(lblTapped(_:))))
        tapGesture.numberOfTapsRequired = 1
        lbl.addGestureRecognizer(tapGesture)
    }

    @objc func lblTapped(_ sender:Any){
        lbl.hidden = true
        textF.hidden = false
        textF.text = lbl.text
    }

    func textFieldShouldReturn(userText: UITextField) -> Bool {
        userText.resignFirstResponder()
        textF.hidden = true
        lbl.hidden = false
        lbl.text = textF.text
        return true
    }
}

Upvotes: 0

craft
craft

Reputation: 2135

It would be better to make the label a UITextField instead. This gives the appearance of a label, and upon click it's editable.

  1. Create the UITextfield via storyboard
  2. Connect the UITextfield outlet from the storyboard into the view controller file
  3. Assign the value of the outlet with an observer
     @IBOutlet weak var barTextField: UITextField! {
            didSet {
                self.barTextField.delegate = self // for step 4
                barTextField.text = "value here"
            }
        }
  1. Extend the viewcontroller with UITextFieldDelegate, which coincides with the delegate in step 3
  2. Implement several of the optional functions in UITextFieldDelegate, including textFieldShouldReturn to detect when the enter key was pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        barTextField.resignFirstResponder()
        return true
    }

Upvotes: 1

thewils
thewils

Reputation: 21

To add my 2c here and remind me of the style in the latest Stanford University Paul Hegarty videos, the setup can be done in the "didSet" of the label Field - you can also set up different responders for different labels this way:

@IBOutlet weak var lblField: UILabel! {
    didSet {
        let recognizer = UILongPressGestureRecognizer()
        recognizer.addTarget(self, action: #selector(ViewController.lbllongpress))
        lblField.addGestureRecognizer(recognizer)
    }
}

and then the implementation becomes:

func lbllongpress(gesture: UILongPressGestureRecognizer) {
    switch gesture.state {
    case UIGestureRecognizerState.began:
        break;
    case UIGestureRecognizerState.ended:
        // Implementation here...
    default: break
    }
}

Upvotes: 2

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

You can not edit label like you edit textField but when user click on label you can hide label and unhide textField and when user finish entering text you can again hide textField and unhide label and you can assign textField's text to label this way:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var lbl: UILabel!
    @IBOutlet weak var textF: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        textF.delegate = self
        textF.hidden = true
        lbl.userInteractionEnabled = true
        let aSelector : Selector = "lblTapped"
        let tapGesture = UITapGestureRecognizer(target: self, action: aSelector)
        tapGesture.numberOfTapsRequired = 1
        lbl.addGestureRecognizer(tapGesture)
    }

    func lblTapped(){
        lbl.hidden = true
        textF.hidden = false
        textF.text = lbl.text
    }

    func textFieldShouldReturn(userText: UITextField) -> Bool {
        userText.resignFirstResponder()
        textF.hidden = true
        lbl.hidden = false
        lbl.text = textF.text
        return true
    }
}

Hope it will help.

Upvotes: 22

Nilesh Patel
Nilesh Patel

Reputation: 6394

Add TapGesture (UITapGestureRecognizer) to your label. Now when user tap on it you can enable the text field & in the text fields delegate method you can convert it to label.

Upvotes: 0

Related Questions