Obscure a UITextField Password in iOS

To obscure a UITextField password in iOS, you can use the secureTextEntry property. This property allows you to hide the text entered into a text field by showing dots or asterisks instead of actual characters. In most iOS apps, we are required to obscure a UITextField to implement the password feature. This is easy to use the property.

The isSecureTextEntry Property

isSecureTextEntry is a property of the UITextField class in iOS that determines whether the text entered into the text field should be obscured, such as when entering a password. When isSecureTextEntry is set to true, the text entered into the field is hidden from view, usually by displaying dots or asterisks instead of the actual characters.

The isSecureTextEntry property is a boolean value that is false by default. To enable password obscuring for a UITextField, you can set isSecureTextEntry to true, either programmatically or in Interface Builder.


Here's an example of how to obscure a UITextField password −

import UIKit
class TestController: UIViewController {
   override func viewDidLoad() {
   private func initialSetup() {
      view.backgroundColor = .white
      let emailTextfield = UITextField()
      emailTextfield.keyboardType = .emailAddress
      emailTextfield.autocapitalizationType = .none
      emailTextfield.autocorrectionType = .no
      emailTextfield.layer.cornerRadius = 8
      emailTextfield.layer.borderColor = UIColor.lightGray.cgColor
      emailTextfield.layer.borderWidth = 1.0
      emailTextfield.placeholder = "Email Address"
      emailTextfield.textAlignment = .center
      let passwordTextfield = UITextField()
      passwordTextfield.keyboardType = .default
      passwordTextfield.autocapitalizationType = .none
      passwordTextfield.autocorrectionType = .no
      passwordTextfield.layer.cornerRadius = 8
      passwordTextfield.layer.borderColor = UIColor.lightGray.cgColor
      passwordTextfield.layer.borderWidth = 1.0
      passwordTextfield.placeholder = "Enter Password"
      passwordTextfield.textAlignment = .center
      passwordTextfield.isSecureTextEntry = true
      let stackView = UIStackView(arrangedSubviews: [emailTextfield, passwordTextfield])
      stackView.axis = .vertical
      stackView.alignment = .fill
      stackView.distribution = .fillEqually
      stackView.spacing = 10
      stackView.translatesAutoresizingMaskIntoConstraints = false
      stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
      stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
      stackView.widthAnchor.constraint(equalToConstant: 300).isActive = true
      stackView.heightAnchor.constraint(equalToConstant: 100).isActive = true


In this example, we create a new UITextField object called passwordTextfield. We then set the isSecureTextEntry property to true, which obscures the text entered into the field. When the user types a character, a dot or asterisk is displayed in the text field instead of the actual character.

You can also set the secureTextEntry property in Interface Builder by selecting the text field and checking the "Secure Text Entry" checkbox in the Attributes inspector.


The isSecureTextEntry property of the UITextField class in iOS is a boolean value that specifies whether the text input into the text field should be hidden. This is done often by showing dots or asterisks instead of real characters. When entering passwords or other sensitive information, this can help with security.

It's crucial to remember that password concealment does not offer total protection. This is because a determined attacker with access to the device may still view the hidden information. Therefore, it's crucial to adopt additional security precautions, such as encryption and safe storage, while managing sensitive information.

Updated on: 04-May-2023


Kickstart Your Career

Get certified by completing the course

Get Started