问题1:切换显隐时,光标会错位的问题
解决办法1:
NSString*string =self.passwordTextField.text;//切换内容后可以从新定位光标位置
self.passwordTextField.text=@"";
self.passwordTextField.text= string;
解决办法2:
textField.enabled =NO;//切换内容后可以从新定位光标位置效果同上
textField.secureTextEntry =YES;
textField.enabled =YES;
[textFieldbecomeFirstResponder];
问题2:切换显隐时,继续输入会将之前的内容清空
解决办法:
#pragma mark ---textField代理方法---
//为textfield从新赋值没有此代理方法则切换显隐后内容会被清空
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange: (NSRange)range replacementString:(NSString*)string
{
if([stringisEqualToString:@" "]) { //不能输入空格
returnNO;
}
//如果是删除键并且内容长度大于0
if([stringisEqualToString:@""] &&self.passwordTextField.text.length>0) {
NSString*stringtemp = [self.passwordTextField.textsubstringToIndex:self.passwordTextField.text.length-1];
self.passwordTextField.text= stringtemp;
returnNO;
}
NSString*stringTemp = [NSStringstringWithFormat:@"%@%@",textField.text,string];
self.passwordTextField.text= stringTemp;
return NO;
}
网友评论