May 312014

UITextField’s Limitation

UITextField has a property called secureTextEntry that hides the characters as you type them (except for the last one, which is hidden after a brief moment). This is useful for things like a password field.

However, you will notice that the keyboard icon that lets you choose languages is absent. This means that you are limited to using English passwords. It is strange that Apple does not support this (FYI, Android does support it).


The only solution I saw was this stackoverflow answer. However, it does not work for all languages. Languages like Korean (Hangul) has a composite character where each letter is composed of multiple symbols. For example, ‘ㅁ’, ‘ㅏ’ and ‘ㄴ’ are all individual characters, but when combined, it becomes ‘만’, which is treated as a single letter.

This means that NSString methods like stringByReplacingCharactersInRange:withString used in the solution in the stackoverflow post does not give the right output.

The Solution

The solution I came up with places a UILabel on top of a UITextField. The UILabel obscures the UITextField, thus hiding the characters being typed in the UITextField. I listen for the UITextFieldTextDidChangeNotification and set UILabel’s text with the appropriate number of “●” (UITextField’s length – 1), except for the last character which is shown. The last character is hidden after a second later if there is no further input.

The cursor is emulated with a ‘|’ character. The blinking is achieved with a NSTimer that toggles the cursor character between a space and a ‘|’.

Demo and Source

You can find the full source along with a sample (the one shown in the video) in my Github repository.

 Posted by at 12:16 am

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>