Ganesh
Ganesh

Reputation: 1919

UITextVIew secureTextEntry not working

Possible Duplicates Here & here (But they don't have solution for my problem)

I am creating a chat UI where my textbox MUST have both Multiple Line feature and Secure Text Entry Feature.

UITextField doesn't have Multiple Line Feature

UITextView doesn't have Secure Text Entry Feature

What should i do now ?

Another doubt, if secureTextEntry for UITextView not going to work, why the heck they put that property inside UITextView Class

Upvotes: 4

Views: 2928

Answers (3)

Ángel Téllez
Ángel Téllez

Reputation: 1016

I made it work by subclassing UITextView and adding to it some properties similar to the ones Pushkraj added. It perfectly works in many scenarios (... at least at the time writing this post).

My whole answer is here.

Upvotes: 0

Pushkraj Lanjekar
Pushkraj Lanjekar

Reputation: 2294

You can use hack for it. You can use predefined delegate method of UITextView and which is textViewDidChange So maintain one flag which will keep state of whether you want secured entry or not. e.g.,

NSString *originalText = @"";
BOOL isSecuredEntryOn = false;

Change it to true whenever you want to add secured text like BOOL isSecuredEntryOn = true;

Now this will be code of your delegate method:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    originalText = [originalText stringByAppendingString:text];
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    NSString *enteredText = textView.text;
    textView.text = (isSecuredEntryOn) ? [enteredText stringByReplacingOccurrencesOfString:enteredText withString:[self getSecuredTextFor:enteredText]] : enteredText;
}

-(NSString *)getSecuredTextFor:(NSString *)stringToConvert {
    NSString *securedString = @"";
    for (int i = 0; i < stringToConvert.length; i++) {
        securedString = [securedString stringByAppendingString:@"*"];
    }
    return securedString;
}

You can use anything instead of * for secured text.

Upvotes: 0

Matloob Hasnain
Matloob Hasnain

Reputation: 1037

Only UITextField supports secureTextEntry, making a font that consists entirely of asterisks and use it in text view once you select secure option. Also be sure to disable copying.

View this link for better understanding

Upvotes: 2

Related Questions