-2

I want to tap on the textfield or the tableview cell and the placeholder text jumps up and become smaller in size. Do you know if i should use tableview or textfield for this?? I have seen this in couple sign up forms of application and i was wondering how i can implement the same thing in my app.

Ava
  • 11
  • 3
  • Welcome to Stack Overflow. Please read the [help] before posting to understand the guidelines. This question is considered too broad for this site. – Charlie Fish Jan 08 '19 at 16:33
  • I tried to post a picture of what i have in mind but sustem doesnt allow me. Since it s my first question. Can i send the image to u or post it somewhere? – Ava Jan 08 '19 at 16:36
  • Have you seen a smiliar app anywhere? Can you arvice me whether i should use textfield or tableview for this? – Ava Jan 08 '19 at 16:54
  • @Ava I got interested so I'll try to implement this – schmidt9 Jan 08 '19 at 17:24
  • https://stackoverflow.com/questions/50773786/how-to-add-a-label-to-textfield-class-or-animate-placeholder I have found this but first i dont want to use cocapods and second the answer given here is too long and complicated: – Ava Jan 08 '19 at 17:41

1 Answers1

0

Here is a basic solution, we do not use constraints as per your linked answer here

EDIT

Refactored, fixed case when we clear text field

    class ViewController2: UIViewController, UITextFieldDelegate {

    @IBOutlet var textField: UITextField!

    var label: UILabel!

    override func viewWillAppear(_ animated: Bool)
    {
        setup()
    }

    func setup()
    {
        self.view.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(viewTap(sender:))))

        textField.placeholder = "My Placeholder"
        textField.delegate = self;

        label = UILabel.init(frame: textField.frame)
        label.font = textField.font;
        label.textColor = UIColor.lightGray
        label.text = textField.placeholder
        label.isHidden = true
        self.view.addSubview(label)
    }

    @objc func viewTap(sender: UITapGestureRecognizer)
    {
        self.view.endEditing(true)
    }

    // MARK: - UITextFieldDelegate

    func textFieldDidBeginEditing(_ textField: UITextField)
    {
        label.isHidden = false;

        if (textField.text?.isEmpty ?? false) {
            textField.placeholder = nil;

            UIView.animate(withDuration: 0.2, animations: {

                self.label.frame = CGRect.init(
                    x: self.textField.frame.origin.x - self.label.frame.size.width * 0.1,
                    y: self.label.frame.origin.y - self.label.frame.size.height,
                    width: self.label.frame.size.width,
                    height: self.label.frame.size.height)

                let transform = CGAffineTransform(scaleX: 0.8, y: 0.8);
                self.label.transform = transform;
            })
        }
    }

    func textFieldDidEndEditing(_ textField: UITextField)
    {
        if (textField.text?.isEmpty ?? false) {
            UIView.animate(withDuration: 0.2, animations: {
                self.view.layoutIfNeeded()
                let transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
                self.label.transform = transform;
                self.label.frame = self.textField.frame
            }) { (Bool) in
                textField.placeholder = self.label.text;
                self.label.isHidden = true;
            }
        }
    }

}
schmidt9
  • 4,436
  • 1
  • 26
  • 32
  • Let me check it in few hours. I stepped out of my computer. I ll get back to you soon – Ava Jan 08 '19 at 18:54
  • can i ask a question regarding the same subject here? – Ava Jan 10 '19 at 14:10
  • @Ava what the question.? – schmidt9 Jan 10 '19 at 16:41
  • so I managed to modify your code for two uitextfields. But I want the label to stay on the top when the uitextfield has a string and disappeares back to the placeholder when there is no string written. I can do this without the animation and just removing the label. I was wondering if you can help me to do the same thing with animation feature. How canI paste my code again? I cannot add it to the comment section – Ava Jan 11 '19 at 00:54
  • @Ava look at fixed answer – schmidt9 Jan 11 '19 at 06:18