在secureTextEntry切换之后,UITextField具有尾随空格 [英] UITextField has trailing whitespace after secureTextEntry toggle

查看:231
本文介绍了在secureTextEntry切换之后,UITextField具有尾随空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在显示/隐藏模式之间切换的按钮(即在secureTextEntry NO和YES之间切换UITextField)。其目的是允许用户查看他们输入的密码。

I have a button that toggles between Show/Hide mode (i.e. toggles a UITextField between secureTextEntry NO and YES). The purpose of which is to allow the user to see the password they are entering.

我在这里跟随了示例(投票数最多):UITextField secureTextEntry - 工作从YES到NO,但是改回YES没有效果

I followed the example (with the highest number of votes) here: UITextField secureTextEntry - works going from YES to NO, but changing back to YES has no effect

然而,当我将secureTextEntry设置为NO时,任何写在那里的文本都会以最后的空格结束。将secureTextEntry设置为YES时,这似乎不是问题。

However, when I set secureTextEntry to NO, any text that was written there ends up with a space at the end. This does not seem to be a problem when setting secureTextEntry to YES.

例如,如果我在setSecureTextEntry设置为NO时输入文本mypassword,然后将其切换为YES,则用户将看到****** ****(10点),这是正确的。如果我将SecureTextEntry设置为NO,则用户将看到mypassword(末尾有空格,或者至少光标向右移动一个空格)。

For example, if I enter the text "mypassword" while setSecureTextEntry is set to NO, and then switch it to YES, the user will see ********** (10 dots), which is correct. If I setSecureTextEntry to NO, the user will see "mypassword " (with a space at the end, or at least, the cursor moved one space to the right).

重要说明:在调试器中,文本的字符串值显示没有尾随空格,如下所示:

Important note: In the debugger, the string value of text appears without the trailing space, like this:

(lldb) expr self.passwordText.text
(NSString *) $0 = 0x1d8450e0 @"mypassword"

我试过修剪空格(每避免UITextField中的中间空格),但它没有效果。

I have tried trimming whitespace (per avoid middle whitespace in UITextField), but it has had no effect.

推荐答案

我刚遇到这个案子,最后解决了这个问题。

i've just encounter this case and finally solved this problem.

适用于最新的iOS SDK,iOS 8.1

works on Latest iOS SDK, iOS 8.1

首先,根本没有尾随空格。

First of all, there is no trailing space at all.

点(显示在SecureEntry中)字符和普通字符的宽度不同,在切换isSecureEntry开关后,光标没有刷新它的位置。

The dot(shown in SecureEntry) character and normal character have different width and after you toggle isSecureEntry switch, the cursor didn't refresh it's position.

因此我使用此解决方法来解决此问题。

so i use this workaround to solved this problem.

- (void)toggle
{
    NSString *tmpString;
    [self.passwordTextField setSecureTextEntry:!self.passwordTextField.isSecureTextEntry];
    if (self.passwordTextField.isSecureTextEntry) {
       // do stuffs
    } else {
       // do other stuffs
    }
    // Workaround to refresh cursor
    tmpString = self.passwordTextField.text;
    self.passwordTextField.text = @" ";
    self.passwordTextField.text = tmpString;
}

Swift 3 +

   // Workaround to refresh cursor

    let currentText: String = self.userPassword.text!
    self.userPassword.text = "";
    self.userPassword.text = currentText

希望它有所帮助!

这篇关于在secureTextEntry切换之后,UITextField具有尾随空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆