正常號轉(zhuǎn)銀行卡號 - 增加4位間的空格
-(NSString *)normalNumToBankNum:(NSString*)number
{
NSString *tmpStr = number;
NSInteger size = (tmpStr.length / 4);
NSMutableArray *tmpStrArr = [[NSMutableArray alloc] init];
for (int n = 0;n < size; n++)
{
[tmpStrArr addObject:[tmpStr substringWithRange:NSMakeRange(n*4, 4)]];
}
[tmpStrArr addObject:[tmpStr substringWithRange:NSMakeRange(size*4, (tmpStr.length % 4))]];
tmpStr = [tmpStrArr componentsJoinedByString:@" "];
return tmpStr;
}
輸入框每4位加一個空格
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == numTF) {
//只允許數(shù)字輸入
NSUInteger lengthOfString = string.length;
for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {//只允許數(shù)字輸入
unichar character = [string characterAtIndex:loopIndex];
if (character < 48) return NO; // 48 unichar for 0
if (character > 57) return NO; // 57 unichar for 9
}
// 四位加一個空格
NSString *temp = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([string isEqualToString:@""]) { // 刪除字符
if ((textField.text.length - 2) % 5 == 0) {
textField.text = [textField.text substringToIndex:textField.text.length - 1];
}
return YES;
} else {
if (textField.text.length % 5 == 0) {
textField.text = [NSString stringWithFormat:@"%@ ", textField.text];
if (temp.length > 25) {
textField.text = [temp substringToIndex:25];
[self.view.window makeToast:@"最多25個字符"];
return NO;
}else{
return YES;
}
}
}
return YES;
}
}
最后編輯于 :
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者