更改UITextfield中的安全密码字符 [英] change the secure password character in UITextfield
问题描述
是否可以更改显示的安全密码字符?
Is it possible to change the secure password character displayed ?
推荐答案
假设Bobby B是对的,那是不可能的改变秘密角色,
我在此提出,
没有任何保证,并且有适用的所有警告,
因为这是一个可怕的kludge 肯定会让你头疼,
希望不要过多投票:
Assuming Bobby B is right in that it is impossible to change the secret character,
I hereby present,
with no guarantuee whatsoever and with all warnings that are applicable,
since this is a horrible kludge which will certainly give you headaches at some point,
hoping not to be downvoted too much for it:
/* don't ever use this PROOF OF CONCEPT CODE for production use */
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ( self.backing == nil ) self.backing = @"";
static BOOL pasting = false;
if ( !pasting )
{
self.backing = [self.backing stringByReplacingCharactersInRange:range withString:string];
NSLog(@"backing: %@",self.backing);
if ( [string length] == 0 ) return YES; // early bail out when just deleting chars
NSString *sec = @"";
for ( int i=0;i<[string length]; i++ ) sec = [sec stringByAppendingFormat:@"■"];
pasting = true;
[[UIPasteboard generalPasteboard] setString:sec];
[textField paste:self];
return NO;
} else {
pasting = false;
return YES;
}
}
/* you have been warned */
将 UITextField
的委托设置为包含此函数的类。
Set your UITextField
's delegate to the class containing this function.
确保您有 self.backing
这是一个保留的 NSString *
。需要使用复制和粘贴kludge来保留光标位置。
Just make sure you have self.backing
which is a retained NSString*
. The copy&paste kludge was needed to preserve cursor position.
我对粘贴一无所知,这是一个疯狂的猜测它可行,但你确实需要找出这是否会导致这方面的问题。我测试了一点,似乎没有任何问题。
I don't know anything about pasting, this was a wild guess and it works, but you do need to find out if this may lead to some problem in that respect. I tested a little and it didn't seem to give any problem.
这篇关于更改UITextfield中的安全密码字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!