我有一个文本字段,当密码隐藏时,我想将默认点字符替换为其他字符。有什么办法可以轻松做到这一点?
这里有2个选项:
这是代码(将密码显示为$$$$):
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 }
``` @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 ```