UITextVIew secureTextEntry not working

≯℡__Kan透↙ 提交于 2019-12-22 06:40:22

问题


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


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/41440781/uitextview-securetextentry-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!