iOS-用另一种形式的文本替换文本

发布于 2025-01-03 22:20:33 字数 507 浏览 1 评论 0原文

我正在为用户创建一个登录页面,并且有一个 UITextField 供用户输入密码。最后我还有一个摘要页面,它返回用户信息,包括密码。我正在尝试将密码更改为显示为“安全”或使用星号而不是文本。例如:

passInput.text = @"password"
changed to: ********;

我使用以下代码来完成任务:

NSString *password = passInput.text;
NSRange range = NSMakeRange(0,passInput.text.length);
NSString *formattedPassword = [password stringByReplacingCharactersInRange:range withString:@"*"];
[sumPassword setText:newPassText];

问题是它只返回一颗星,而不是我认为应该返回的 8 颗星。

我缺少什么?

I'm creating a login page for users, and I have a UITextField for users to input a password. I also have a summary page at the end, which gives back the users information including the password. I'm trying to change the password to display as "secure" or with stars instead of the text. For example:

passInput.text = @"password"
changed to: ********;

I am using the following code to accomplish the task:

NSString *password = passInput.text;
NSRange range = NSMakeRange(0,passInput.text.length);
NSString *formattedPassword = [password stringByReplacingCharactersInRange:range withString:@"*"];
[sumPassword setText:newPassText];

The problem is that it returns only one star and not 8 stars as I would think it should.

What am I missing?

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

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

发布评论

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

评论(2

千里故人稀 2025-01-10 22:20:33

您可以设置:

passInput.secureTextEntry = YES;

以便在该字段中书写时显示点而不是字母。

否则你可以像这样:

NSUInteger length = passInput.text.length;
NSMutableString * str = [[NSMutableString alloc] init];

for (NSUInteger i = 0 ; i < length ; ++i) {
    [str appendString:@"*"];
}

或者...

NSUInteger length = passInput.text.length;
// Make this long enough
NSString * str = @"*********************************************************";
NSString * pass = [str substringToIndex:length];

You could just set:

passInput.secureTextEntry = YES;

so that it shows dots instead of letters when writing in that field.

Otherwise you could make it like:

NSUInteger length = passInput.text.length;
NSMutableString * str = [[NSMutableString alloc] init];

for (NSUInteger i = 0 ; i < length ; ++i) {
    [str appendString:@"*"];
}

or...

NSUInteger length = passInput.text.length;
// Make this long enough
NSString * str = @"*********************************************************";
NSString * pass = [str substringToIndex:length];
情栀口红 2025-01-10 22:20:33

我假设您需要给它提供与范围内的字符一样多的 * ,否则它会取出整个范围,并将其替换为给定的字符串。 ,您必须将其更改为

NSString *formattedPassword = [password stringByReplacingCharactersInRange:range withString:@"********"];

换句话说,对于给定的 password = @"password"; 示例

I assume you need to give it as many *'s as you have characters in the range, otherwise its taking out that entire range, and replacing it with the string given. In other words, you would have to change it to

NSString *formattedPassword = [password stringByReplacingCharactersInRange:range withString:@"********"];

for the given example of password = @"password";

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