Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

iphone - change the secure password character in UITextfield

Is it possible to change the secure password character displayed ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 */

Set your UITextField's delegate to the class containing this function.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...