change the secure password character in UITextfiel

2020-01-31 03:37发布

Is it possible to change the secure password character displayed ?

4条回答
【Aperson】
2楼-- · 2020-01-31 04:23

You could also just change the font...

If you change the unicode character 'BULLET' (U+2022) you can customise the secret character. There are a lot of free font editor out there. I use this one http://www.cr8software.net/typex.html

To add custom fonts to your project, check out - this link

It's quite easy

Then you don't have to code anything it just works.

查看更多
可以哭但决不认输i
3楼-- · 2020-01-31 04:23

Simply, allow Secure property in IB or via program.

查看更多
孤傲高冷的网名
4楼-- · 2020-01-31 04:31

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.

查看更多
forever°为你锁心
5楼-- · 2020-01-31 04:33

I don't think that this is possible, and I'm basing that on a lack of documentation found in the docs for UITextInputTraits (specifically for secureTextEntry).

Could be wrong though - interested to hear if someone has done this before.

Good luck!

查看更多
登录 后发表回答