简体   繁体   中英

How to move/animate UITextField placeholder text

On many applications the placeholder text both changes, and animates when the text field is selected. Is this ability stemming from some property of UITextField.placeholder or are the developers using another view and animating that.

If you want nice animation same as you describe into your question, then try once following third party repository for UITextField.

RPFloatingPlaceholders
JVFloatLabeledTextField
UIFloatLabelTextField
FloatLabelFields

If you are looking for the UITextView equivalent of this animation, please visit UIFloatLabelTextView repository.

To animate UITextField place holder you can try this approach:

func animatePlaceHolder(_ textField: UITextField) {
    // Get the placeHolder reference
    let placeHolderLabel = textField.subviews.first(where: { NSStringFromClass(type(of: $0)) == "UITextFieldLabel" })

    // animate
    UIView.animate(withDuration: 1.0) {
        // Animate place holder properties here
    }
}

You can call this function in textFieldDidBeginEditing(_ textField: UITextField) method. You can also create a UITextField extension with this function, so you won't have to copy paste it in each viewController where you need to animate the placeHolders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM