面临同样的问题。这是我的理解和解决方案。
更改 clearsOnInsertion、clearsOnBeginEditing 对安全文本字段没有影响。
在文本字段上切换 secureTextEntry 会导致它失去响应者状态并再次成为第一响应者。因此,任何新文本都会清除当前文本。
出于安全原因,我通过覆盖 textField:shouldChangeCharactersInRange:replacementString: 和其他一些更改来解决它。我在编辑时使用右视图显示切换按钮。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// Customize text entry in password field
if (textField == self.passwordTf) {
NSString *currentString = textField.text;
// Handle inserting, deleting characters
textField.text = [currentString stringByReplacingCharactersInRange:range withString:string];
// Setting the cursor at the right place
NSRange selectedRange = NSMakeRange(range.location + string.length, 0);
UITextPosition* from = [textField positionFromPosition:textField.beginningOfDocument offset:selectedRange.location];
UITextPosition* to = [textField positionFromPosition:from offset:selectedRange.length];
textField.selectedTextRange = [textField textRangeFromPosition:from toPosition:to];
// If password cleared, updated edited status
if (textField.text.length == 0) {
passwordEdited = YES;
}
[self toggleButtonStatus];
return NO;
}
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
// Reset password edited status on begin editing
if (textField == self.passwordTf &&
[textField.text length] > 0) {
passwordEdited = NO;
}
[self toggleButtonStatus];
}
- (void)textFieldDidChange:(NSNotification*)notification
{
UITextField *textField = notification.object;
// If password cleared, updated edited status
if (self.passwordTf == textField &&
textField.text.length == 0) {
passwordEdited = YES;
}
[self toggleButtonStatus];
}
- (void)toggleButtonStatus
{
// For security, only show eye button if not saved password
if (passwordEdited) {
self.passwordTf.clearButtonMode = UITextFieldViewModeNever;
self.passwordTf.rightViewMode = UITextFieldViewModeWhileEditing;
} else {
self.passwordTf.clearButtonMode = UITextFieldViewModeWhileEditing;
self.passwordTf.rightViewMode = UITextFieldViewModeNever;
}
}
作为奖励,这是我的切换按钮代码,用于更新切换时的光标位置。
eyeButton = [UIButton buttonWithType:UIButtonTypeCustom];
eyeButton.frame = CGRectMake(0, 0, 44, 44);
eyeButton.imageEdgeInsets = UIEdgeInsetsMake(0, 16, 0, 0);
[eyeButton addTarget:self action:@selector(eyeButtonPressed:) forControlEvents:UIControlEventTouchDown];
[eyeButton addTarget:self action:@selector(eyeButtonReleased:) forControlEvents:UIControlEventTouchUpInside];
[eyeButton addTarget:self action:@selector(eyeButtonReleased:) forControlEvents:UIControlEventTouchUpOutside];
[eyeButton addTarget:self action:@selector(eyeButtonReleased:) forControlEvents:UIControlEventTouchCancel];
[eyeButton addTarget:self action:@selector(eyeButtonReleased:) forControlEvents:UIControlEventTouchDragExit];
...
self.passwordTf.rightView = eyeButton;
self.passwordTf.rightViewMode = UITextFieldViewModeWhileEditing;
- (void)eyeButtonPressed:(id)sender {
UIFont *textFieldFont = ...
UIColor *textFieldColor = ...
// Hack to update cursor position
self.passwordTf.defaultTextAttributes = @{NSFontAttributeName: textFieldFont,
NSForegroundColorAttributeName: textFieldColor};
// Change secure entry
self.passwordTf.secureTextEntry = NO;
}
- (void)eyeButtonReleased:(id)sender {
UIFont *textFieldFont = ...
UIColor *textFieldColor = ...
// Hack to update cursor position
self.passwordTf.defaultTextAttributes = @{NSFontAttributeName: textFieldFont,
NSForegroundColorAttributeName: textFieldColor};
// Change secure entry
self.passwordTf.secureTextEntry = YES;
}
如果您需要任何说明或发现任何错误,请告诉我。 ;)
尽情享受吧!