一尘不染

如何在iOS Swift的密码字段中将点更改为另一个字符

swift

我有一个文本字段,当密码隐藏时,我想将默认点字符替换为其他字符。有什么办法可以轻松做到这一点?


阅读 235

收藏
2020-07-07

共1个答案

一尘不染

这里有2个选项:

  1. 使用没有安全输入选项的普通文本字段。用户输入字符时,将其保存到字符串变量,然后在文本字段中将其替换为您希望显示的字符而不是项目符号。

这是代码(将密码显示为$$$$):

var password: String = ""
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    password = password+string
    textField.text = textField.text+"$"
    println("\(password)")
    return false
}
  1. 在此处查看答案:

```
@interface SecureTextFieldWithCustomFont : UITextField
@property (nonatomic) BOOL secure;
@property (nonatomic, strong) NSString *actualText;
@end

@implementation SecureTextFieldWithCustomFont

-(void)awakeFromNib
{
[super awakeFromNib];

if (self.secureTextEntry)
{
    // Listen for changes.
    [self addTarget:self action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
    [self addTarget:self action:@selector(editingDidChange) forControlEvents:UIControlEventEditingChanged];
    [self addTarget:self action:@selector(editingDidFinish) forControlEvents:UIControlEventEditingDidEnd];
}

}

-(NSString*)text
{
if (self.editing || self.secure == NO)

else
{ return self.actualText; }

}

-(void)editingDidBegin
{
self.secureTextEntry = YES;
self.text = self.actualText;
}

-(void)editingDidChange

-(void)editingDidFinish
{
self.secureTextEntry = NO;
self.actualText = self.text;
self.text = [self dotPlaceholder];
}

-(NSString)dotPlaceholder
{
int index = 0;
NSMutableString
dots = @”“.mutableCopy;
while (index < self.text.length)
{ [dots appendString:@”•”]; index++; }
return dots;
}

@end
```

2020-07-07