Obscure a UITextField password
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Setting isSecureTextEntry = true on a UITextField obscures the password by replacing characters with dots. This is the standard iOS approach and also disables text selection, auto-correction, and screenshot capture of the field content. For a password visibility toggle (show/hide button), combine isSecureTextEntry with a toggle button that switches the property at runtime. SwiftUI provides the built-in SecureField view for the same purpose.
Basic Password Field (UIKit)
Setting isSecureTextEntry = true does the following:
- Replaces each character with a bullet dot after a brief moment
- Disables text selection and copy/paste
- Prevents the field content from appearing in screenshots
- Disables predictive text and autocorrection
Interface Builder (Storyboard)
In Interface Builder, select the UITextField and in the Attributes Inspector:
- Check "Secure Text Entry" under the Text Input Traits section
- Set "Content Type" to "Password" for Keychain integration
- Set "Autocorrection" to "No"
- Set "Autocapitalization" to "None"
Password Visibility Toggle
Handling the Cursor Position Bug
When toggling isSecureTextEntry while the field is focused, the cursor may jump to the beginning. The fix:
SwiftUI SecureField
Objective-C
Password Strength Indicator
Common Pitfalls
- Toggling isSecureTextEntry resets the cursor: When switching between secure and plain text while the field is focused, iOS may reset the cursor to the beginning or clear the text. Save and restore the text and cursor position after toggling to avoid user frustration.
- Not setting textContentType to .password: Without
textContentType = .password, iOS does not offer Keychain autofill for the field. Users expect the password autofill prompt, especially with Face ID/Touch ID integration. Set it to.passwordfor login and.newPasswordfor registration. - Allowing autocorrection on password fields: Autocorrection caches typed text in the keyboard dictionary, potentially leaking password fragments. Always set
autocorrectionType = .noandautocapitalizationType = .noneon password fields. - Using custom font with secure entry: Setting a custom font on a secure text field may cause the bullet dots to render at the wrong size or position on some iOS versions. Test with your custom font, or use the system font for the secure state.
- Screenshots still capturing the field in SwiftUI:
SecureFieldin SwiftUI hides content from screenshots, but switching to aTextFieldfor the "show password" state makes it capturable again. If screenshot protection is required, keep the field asSecureFieldand disable the show toggle.
Summary
- Set
isSecureTextEntry = trueonUITextFieldto obscure password characters with dots - Use
SecureFieldin SwiftUI for the same functionality with less code - Add a toggle button to switch between
isSecureTextEntry = true/falsefor show/hide password - Set
textContentType = .passwordto enable Keychain autofill and Face ID/Touch ID - Disable autocorrection and autocapitalization on all password fields
- Fix the cursor position bug when toggling secure entry by resetting the text range
Related reading
- On logout, clear Activity history stack, preventing back button from opening logged-in-only Activities
- One istio-ingressgateway and multiple TLS gateways
- Openshift .kubeconfig file and certificate authentication
- OpenShift Route Certificate auto renew
- Obtain Bundle Identifier programmatically
- Obtain bundle identifier programmatically in Swift?
- Optimistic concurrency control clarification
- Optimistic vs. Pessimistic locking

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.