这里和这里可能有重复项(但他们没有解决我的问题)
我正在创建一个聊天 UI,其中我的文本框必须同时具有多行功能和安全文本输入功能。
UITextField 没有多行功能
UITextView 没有安全文本输入功能
我现在该怎么办?
另一个疑问是,如果 UITextView 的 secureTextEntry 不起作用,为什么他们把这个属性放在 UITextView 类中
只有UITextField支持secureTextEntry,使字体完全由星号组成,并在选择安全选项后在文本视图中使用它。另外,请务必禁用复制。
查看此链接以更好地理解
你可以使用hack。您可以使用预定义的delegate
UITextView
方法,这是textViewDidChange
因此,维护一个标志,该标志将保持您是否要安全进入的状态。例如,
NSString *originalText = @"";
BOOL isSecuredEntryOn = false;
每当要添加安全文本时,请将其更改为true
,例如BOOL isSecuredEntryOn = true;
现在这将是您的delegate
方法的代码:
- (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;
}
对于安全文本,您可以使用任何内容代替 *。
我通过子类化UITextView
并向其添加一些类似于 Pushkraj 添加的属性来使其工作。它在许多情况下都能完美运行(...至少在写这篇文章的时候)。
我的全部答案就在这里。