Short answer

You don't need a CocoaPod for a text field whose placeholder moves above the text. Subclass UITextField, add a UILabel for the title, and move it up with a scale-and-move transform when the field is focused or has text. Override textRect(forBounds:) to leave room above the text. Also update the title when text is set in code, because that sends no editing event. A show-password button is a rightView that toggles isSecureTextEntry.

SWIFT TIPS · UIKIT · UITEXTFIELD

First posted on June 4, 2018, as "Simple Animation TextField Swift 4". Rewritten on November 18, 2026. The field was run on a Mac as a Mac Catalyst program, which runs UIKit on macOS (not an iPhone or the simulator), with Swift 6.2.3 (Xcode 26.2) on macOS 26.0.1, so the sizes below use the Mac's fonts. The test had no window, so the field never became focused and the focus animation wasn't tested. The code was type-checked against the iOS 26.2 SDK in Swift 5 and Swift 6 modes with iOS 15 and iOS 26.2 as deployment targets, but not run on a device.

In 2018 I wanted a password field with a placeholder that slides up when you start typing and a button to show the password. The post used the AnimatedTextInput CocoaPod: install it, set placeHolderText, set type = .password(toggleable: true), and give it a style. The style was a struct with twenty properties for colors, fonts, margins and offsets.

The library did the job. But that look is a small amount of UIKit, and writing it yourself removes a dependency and lets you decide how it handles larger text sizes, Reduce Motion and VoiceOver. Here is the whole field.

The field

import UIKit

/// A text field whose placeholder floats above the text when the field is focused or filled,
/// with an underline that changes color and an optional show/hide button for passwords.
final class FloatingLabelTextField: UITextField {
    var title = "" {
        didSet { titleLabel.text = title; accessibilityLabel = title }
    }
    var activeColor: UIColor = .systemGreen { didSet { updateLook(animated: false) } }
    var inactiveColor: UIColor = .secondaryLabel { didSet { updateLook(animated: false) } }

    private let titleLabel = UILabel()
    private let underline = UIView()
    private let floatScale: CGFloat = 0.75
    private let topSpace: CGFloat = 18           // room above the text for the floated title

    override var text: String? {
        didSet { updateLook(animated: false) }   // setting text in code doesn't send editingChanged
    }

    /// True while the title sits above the text.
    var isTitleFloating: Bool { isFirstResponder || !(text ?? "").isEmpty }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUp()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUp()
    }

    private func setUp() {
        borderStyle = .none
        font = .preferredFont(forTextStyle: .body)
        adjustsFontForContentSizeCategory = true
        titleLabel.font = font
        titleLabel.isAccessibilityElement = false   // the field reads its title as its label
        addSubview(titleLabel)
        addSubview(underline)
        addAction(UIAction { [weak self] _ in self?.updateLook(animated: true) }, for: [.editingDidBegin, .editingDidEnd])
    }

    /// Adds an eye button that shows or hides the password.
    func makePasswordField() {
        isSecureTextEntry = true
        textContentType = .password
        let button = UIButton(type: .system)
        button.setImage(UIImage(systemName: "eye"), for: .normal)
        button.accessibilityLabel = "Show password"
        button.addAction(UIAction { [weak self, weak button] _ in
            guard let self, let button else { return }
            self.isSecureTextEntry.toggle()
            button.setImage(UIImage(systemName: self.isSecureTextEntry ? "eye" : "eye.slash"), for: .normal)
            button.accessibilityLabel = self.isSecureTextEntry ? "Show password" : "Hide password"
        }, for: .touchUpInside)
        rightView = button
        rightViewMode = .always
    }

    override var intrinsicContentSize: CGSize {
        CGSize(width: UIView.noIntrinsicMetric, height: topSpace + (font?.lineHeight ?? 20) + 10)
    }

    // Text and editing areas sit below the floated title.
    override func textRect(forBounds bounds: CGRect) -> CGRect {
        var rect = super.textRect(forBounds: bounds)
        rect.origin.y = topSpace
        rect.size.height = bounds.height - topSpace - 2
        return rect
    }
    override func editingRect(forBounds bounds: CGRect) -> CGRect { textRect(forBounds: bounds) }
    override func placeholderRect(forBounds bounds: CGRect) -> CGRect { textRect(forBounds: bounds) }

    override func layoutSubviews() {
        super.layoutSubviews()
        let textArea = textRect(forBounds: bounds)
        // Size and place the label as if it were resting in the text area, then transform it.
        titleLabel.transform = .identity
        titleLabel.frame = CGRect(x: textArea.minX, y: textArea.minY, width: textArea.width, height: textArea.height)
        underline.frame = CGRect(x: 0, y: bounds.height - 1, width: bounds.width, height: isFirstResponder ? 2 : 1)
        updateLook(animated: false)
    }

    private func updateLook(animated: Bool) {
        let floating = isTitleFloating
        let changes = {
            if floating {
                // Shrink toward the leading edge and center the title in the top space.
                let textArea = self.textRect(forBounds: self.bounds)
                let dx = -textArea.width * (1 - self.floatScale) / 2
                let dy = self.topSpace / 2 - textArea.midY
                self.titleLabel.transform = CGAffineTransform(translationX: dx, y: dy).scaledBy(x: self.floatScale, y: self.floatScale)
            } else {
                self.titleLabel.transform = .identity
            }
            self.titleLabel.textColor = self.isFirstResponder ? self.activeColor : self.inactiveColor
            self.underline.backgroundColor = self.isFirstResponder ? self.activeColor : self.inactiveColor.withAlphaComponent(0.3)
        }
        if animated, !UIAccessibility.isReduceMotionEnabled {
            UIView.animate(withDuration: 0.2, delay: 0, options: [.beginFromCurrentState], animations: changes)
        } else {
            changes()
        }
    }
}

final class TextInputViewController: UIViewController {
    private let emailField = FloatingLabelTextField()
    private let passwordField = FloatingLabelTextField()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground

        emailField.title = "Email"
        emailField.keyboardType = .emailAddress
        emailField.textContentType = .username
        emailField.autocapitalizationType = .none
        passwordField.title = "Password"
        passwordField.makePasswordField()

        let stack = UIStackView(arrangedSubviews: [emailField, passwordField])
        stack.axis = .vertical
        stack.spacing = 20
        stack.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stack)
        NSLayoutConstraint.activate([
            stack.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 32),
            stack.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
            stack.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
        ])
    }
}

What the test showed

I created the field at 300 points wide, made it a password field, and read the title label's frame in each state:

Field height 48 pt, text area y 18...46
Empty, not focused: floating false, title frame y 18...46 x 0, scale 1.0
Text set in code   : floating true, title frame y -1...19 x 0, scale 0.75
Floated title glyph area y 1.5...16.5 (top space is 0...18)
Text cleared       : floating false, title frame y 18...46 x 0, scale 1.0
Secure: true, button label "Show password"
After tap: secure false, button label "Hide password", text "secret-1"
After second tap: secure true, text "secret-1"
Field accessibilityLabel: Password, title label is accessibility element: false
Plain UITextField, text set in code: 0 editing events
  • At rest, the title acts as the placeholder. It sits in the text area at full size.
  • With text, it floats. At 75% size its letters fit between 1.5 and 16.5 points, inside the 18 points kept free above the text.
  • Text set in code needs its own update. The last line is a plain UITextField: setting text in code sent no editing event at all. Without the didSet on text, a value you fill in for the user, like a saved email address, would be drawn on top of the title.
  • The eye button switched secure entry off and on, kept the text, and changed its VoiceOver label between "Show password" and "Hide password".
  • VoiceOver reads the title once. The field's accessibility label is the title, and the floating label is hidden from VoiceOver so it isn't read twice.

How it works

  • Room for the title. textRect, editingRect and placeholderRect all return the area below topSpace, so typed text never runs under the floated title.
  • One transform. The label is laid out as if it were resting in the text area. To float, it's scaled to 75%, moved left by the width the scaling took off its leading edge, and moved up so it's centered in the space above the text. Layout stays the same; only the transform changes, and a transform animates smoothly.
  • Animation only when the focus changes, in 0.2 seconds, and not at all when Reduce Motion is on. Text set in code moves the title without animation.
  • Text size. The font is the Body text style with adjustsFontForContentSizeCategory, so the field follows the user's text size setting, and the height comes from the font's line height. If someone changes the setting while the screen is open, call invalidateIntrinsicContentSize() so the height follows.
  • Colors. The default active color is green, like the 2018 style, and the inactive color is .secondaryLabel, which adapts to dark mode.
  • Password AutoFill. makePasswordField() sets textContentType = .password so iOS can offer saved passwords.

The 2018 style, mapped

Most of the twenty style properties become a few lines:

  • placeholderInactiveColor and inactiveColor become inactiveColor.
  • activeColor, lineActiveColor and lineInactiveColor become activeColor and the underline, which is 2 points tall while editing and 1 point otherwise.
  • textInputFont, a fixed 14-point system font, becomes the Body text style, so it grows with the user's setting.
  • placeholderMinFontSize becomes floatScale, and topMargin becomes topSpace.

A quick check for your project

  1. If a text-field library is only there for the floating placeholder, list the style properties you set. Most of them map to two colors, a font and a scale.
  2. Fill the field from code, for example with a saved email, and check that the title moves up.
  3. Turn on Reduce Motion and the largest text size, and check that the field still reads well.

What the 2018 version said

The post's animation is AnimatedTextInput's own demo, from the library's README, which is published under the MIT license:

AnimatedTextInput's demo animation: text fields whose placeholders move up as you type

The code set the placeholder to "Paswword", a typo, and the style used fixed colors such as UIColor.black for the text, which doesn't adapt to dark mode.

Sources