【IOS开发】UItextfield输入电话号码,自动调整格式
UItextfield中实现输入电话号码,自动按位置在加“—”效果。效果图如下。
核心代码:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (range.location == 13) { return NO; }else if (range.location == 8){ NSMutableString *str = [[NSMutableString alloc] initWithString:phoneField.text]; NSRange range = [str rangeOfString:@"-"]; if (range.location!=NSNotFound) { }else { [str insertString:@"-" atIndex:3]; [str insertString:@"-" atIndex:8]; phoneField.text = str; } return YES; }else if(range.location==9) { NSMutableString *str = [[NSMutableString alloc] initWithString:phoneField.text]; NSString *str1; str1 = [str stringByReplacingOccurrencesOfString:@"-"withString:@""]; phoneField.text = str1; return YES; }else { return YES; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。