You cannot edit the label as you edit textField, but when the user clicks on the label, you can hide the label and display textField, and when the user finishes typing, you can hide the text screen again and show the label, and you can set the text to TextField 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 this helps.
source
share