oleghnidets / TweeTextField

Lightweight set of text fields with nice animation and functionality ๐Ÿš€
MIT License
491 stars 59 forks source link

Set `text` programmatically in text field does not move placeholder upward. #8

Closed m-junaid-butt closed 6 years ago

m-junaid-butt commented 6 years ago

I have a scenario in which I don't have to activate the text field. I have set text in it programatically based on some conditions but now the issue is, this library does not support this thing. When I set text programatically in the text field then placeholder label does not move up. Both text & placeholder text show in the field. It would be a great improvement if you can add this feature in your library.

oleghnidets commented 6 years ago

Hi, Thanks for reporting. I will take a look and try to fix ASAP.

oleghnidets commented 6 years ago

@m-junaid-butt Tell me, please, have you updated the library to the latest version?

m-junaid-butt commented 6 years ago

@oleghnidets actually, I forked your repo and made my own changes to achieve that behaviour. Let me change my PodFile to point your updated repo. ๐Ÿ‘

oleghnidets commented 6 years ago

To be honest, I cannot reproduce the issue. Please, download sample project, launch it. Tap Confirm button. Actually, it sets correct placeholder position.

m-junaid-butt commented 6 years ago

@oleghnidets you are right. It is working in new version. Actually, you forgot to mention the pod version in ReadME file. By default, pod was pointing to 1.0.0 version instead of 1.1.1 that's why I was getting this issue. :(

Please update it in the ReadMe file and then close this issue. Thank you so much to quickly respond my issue. :)

oleghnidets commented 6 years ago

@m-junaid-butt Actually you don't have to specify pod version :) You have to use pod update. Read guide for more details.

Minitour commented 6 years ago

Hi @oleghnidets, was this issue fixed? I am using version 1.2.2 and I am still getting the described issue: Setting text on the field programatically in viewDidLoad causes the placeholder label to overlay the text.

public override func viewDidLoad() {
        super.viewDidLoad()

       ...

        countryField.text = countryCode

    }

Do note that my controller is initialised programatically, and the textfield is loaded through the loadView function.

Minitour commented 6 years ago

Upon further inspection of the code, I noticed that minimizePlaceholder is being called, however since we set the text in viewDidLoad the frame is still 0. The current workaround is to set the text from viewDidAppear.

oleghnidets commented 6 years ago

Hi, Take a look, please, at #15 It is a newer version. I will post the update asap.

Now minimizePlaceholder is called only when a user taps on a text field. Although if you want to make the text field first responder you have to do it in viewDidAppear.

Minitour commented 6 years ago

@oleghnidets In

open class TweePlaceholderTextField: UITextField 

When text is set:

open override var text: String? {
        didSet {
            setCorrectPlaceholderSize()
        }
    }

which invokes:

@objc private func setCorrectPlaceholderSize() {
        if let text = text, text.isEmpty == false {
            minimizePlaceholder()
        } else if isEditing == false {
            maximizePlaceholder()
        }
    }

which then calls minimizePlaceholder

@objc private func minimizePlaceholder() {
        bottomConstraint?.constant = -frame.height //<- height is 0

        UIView.animate(withDuration: placeholderDuration, delay: 0, options: [.preferredFramesPerSecond30], animations: {
            self.layoutIfNeeded()

            switch self.minimizationAnimationType {
            case .immediately:
                self.placeholderLabel.font = self.placeholderLabel.font.withSize(self.minimumPlaceholderFontSize)
            case .smoothly:
                self.minimizeFontAnimation.start()
            }
        }, completion: { _ in
            self.minimizeFontAnimation.stop()
        })
    }
Minitour commented 6 years ago

Seems like the code in release 1.2.2 does not match the code in the master branch. Is 1.2.2 the latest release? or am I using an older version?

EDIT: @oleghnidets didn't see the edit. my bad ๐Ÿ˜…thanks for the quick reply!

oleghnidets commented 6 years ago

Currently, it is beta version in master. Sorry, because of lack of time, I cannot manage to update the library.