iOS development
UITextField
password security
app development
Swift programming

Obscure a UITextField password

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To obscure password input in a UITextField, set isSecureTextEntry to true. That tells UIKit to mask the typed characters on screen, which is the standard iOS behavior for password fields and is usually all you need for the visual part of password protection.

Basic Secure Password Field Setup

A simple password field can be configured either in Interface Builder or in code.

swift
1import UIKit
2
3final class LoginViewController: UIViewController {
4    private let passwordField = UITextField()
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8
9        passwordField.placeholder = "Password"
10        passwordField.isSecureTextEntry = true
11        passwordField.borderStyle = .roundedRect
12        passwordField.textContentType = .password
13        passwordField.autocapitalizationType = .none
14        passwordField.autocorrectionType = .no
15        passwordField.spellCheckingType = .no
16    }
17}

The key line is isSecureTextEntry = true, but the surrounding keyboard and autofill settings improve the real user experience.

Why textContentType Matters

textContentType helps iOS understand what kind of data the field contains. For passwords, this can improve password manager integration and system autofill behavior.

For a login form, a common pairing is:

swift
usernameField.textContentType = .username
passwordField.textContentType = .password

For account creation, you may prefer .newPassword on the password field instead.

Add a Show and Hide Toggle

Many apps let users reveal the password briefly to verify what they typed. A simple way is to toggle isSecureTextEntry from a button.

swift
@objc private func togglePasswordVisibility() {
    passwordField.isSecureTextEntry.toggle()
}

If you want a button inside the field:

swift
1let button = UIButton(type: .system)
2button.setTitle("Show", for: .normal)
3button.addTarget(self, action: #selector(togglePasswordVisibility), for: .touchUpInside)
4passwordField.rightView = button
5passwordField.rightViewMode = .always

That gives the user a familiar show and hide pattern.

Be Careful When Toggling While Editing

Toggling secure entry while the field is first responder can produce cursor glitches in some situations. A common workaround is to preserve and restore the text.

swift
1@objc private func togglePasswordVisibility() {
2    let currentText = passwordField.text
3    passwordField.isSecureTextEntry.toggle()
4    passwordField.text = nil
5    passwordField.text = currentText
6}

Not every app needs this workaround, but it is worth knowing if the cursor jumps or the field redraw behaves strangely.

Obscuring Text Is Not Full Security

isSecureTextEntry only changes how the text is displayed on screen. It does not encrypt the string in memory, store it securely, or protect it during network transfer.

Real password handling also requires:

  • secure transport such as HTTPS
  • careful avoidance of debug logging
  • secure server-side handling
  • secure storage only when truly necessary

The text field masking is important, but it is only one layer.

Accessibility and UX Notes

Secure text fields should still be usable. If you add a show and hide button, label it clearly for VoiceOver. Also avoid forcing insecure UX, such as disabling paste without a strong reason.

A good password field balances privacy and usability:

  • masking by default
  • optional reveal control
  • correct keyboard and autofill hints
  • clear validation feedback

Common Pitfalls

A common mistake is setting only isSecureTextEntry and forgetting to disable autocorrection and capitalization, which can interfere with password entry.

Another pitfall is thinking secure text entry alone protects the password completely. It only obscures the on-screen display.

Developers also sometimes toggle secure entry and run into cursor or redraw issues while the field is active. If that happens, preserve and reassign the text.

Finally, if you want iCloud Keychain and password manager integration to work smoothly, do not skip textContentType.

Summary

  • Set passwordField.isSecureTextEntry = true to mask password input.
  • Use textContentType to improve autofill and password manager behavior.
  • Add a show and hide toggle if the UX needs it.
  • Watch for cursor glitches when toggling secure entry while editing.
  • Remember that masking text on screen is only one part of password security.

Course illustration
Course illustration

All Rights Reserved.