/*实现的功能:当输入一个电话号码后,在结束编辑时,变为xxx****xxxx的形式,当想再次编译时,点入后,会还原成输入时的无掩码状态*/
@interface AppDelegate ()
@property (nonatomic,retain) NSString * string;
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[[UIScreenmainScreen] bounds]];
_window.backgroundColor = [UIColor blueColor];
[_window makeKeyAndVisible];
UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;//设置外形
textField.placeholder = @"你好";//占位字符
textField.text = @"18701590789";//默认显示的号码
textField.clearButtonMode = UITextFieldViewModeWhileEditing;//设置清除按钮的显示方式
[textField becomeFirstResponder];//设为第一响应者,使跟光标默认显示在输入框中;
textField.delegate = self;//设置代理
UITextField * textField1 = [[UITextFieldalloc]initWithFrame:CGRectMake(100, 300, 200, 30)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
[_window addSubview:textField1];
//获取输入的字符串;
self.string = textField.text;
[_window addSubview:textField];
[_window release];
return YES;
}
#pragma mark----------------------实现协议中方法----------------------------------
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
textField.text = self.string;//获取更改后的值并显示;还原成原始状态(无掩码显示)
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
self.string = textField.text;
//防止输入的位数不足7位时的越界现象;
if (textField.text.length >= 7) {
self.string = textField.text;//保存原始值,以便原码显示;
//截取前三个字符;
NSString * string1 = [_string substringToIndex:3];
//截取后四位数字:
NSString * string2 = [_string substringFromIndex:7];
NSString * string3 = [NSString stringWithFormat:@"%@****%@",string1,string2];
textField.text = string3;
}
return YES;
}
转载于:https://my.oschina.net/u/2447735/blog/501762