更改 UITextfield 中的安全密码字符

发布于 2024-09-10 03:30:46 字数 23 浏览 2 评论 0原文

是否可以更改显示的安全密码字符?

Is it possible to change the secure password character displayed ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘虞初梦 2024-09-17 03:30:46

假设鲍比 B 是对的,不可能改变秘密角色,
我在此提出,
没有任何保证且有所有适用的警告,
因为这是一个可怕的拼凑,在某些时候肯定会让你头疼,
希望不要对其投太多反对票:

/* 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 委托设置为包含此函数的类。

只需确保您有 self.backing,它是保留的 NSString*。需要复制和粘贴拼凑来保留光标位置。

我对粘贴一无所知,这是一个疯狂的猜测,它确实有效,但您确实需要找出这是否会导致这方面的问题。我测试了一下,似乎没有出现任何问题。

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.

思念绕指尖 2024-09-17 03:30:46

您也可以只更改字体...

如果更改 unicode 字符“BULLET”(U+2022),您可以自定义秘密字符。
有很多免费的字体编辑器。
我使用这个 http://www.cr8software.net/typex.html

来添加自定义字体对于您的项目,请查看 - 此链接< /a>

这非常简单,

然后您无需编写任何代码即可正常工作。

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.

云归处 2024-09-17 03:30:46

我认为这是不可能的,并且我的基础是在文档中缺少 UITextInputTraits (专门针对 secureTextEntry)。

但也可能是错的——有兴趣听听以前是否有人这样做过。

祝你好运!

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!

十年九夏 2024-09-17 03:30:46

简而言之,允许在 IB 中或通过程序保护财产。

Simply, allow Secure property in IB or via program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文