仅支持X位整数Y位小数 的处理:
-(void) handleInPut:(UITextField*)textfield IntBitCount:(NSInteger) intBitCount dotBitCount:(NSInteger)dotBitCount toastMsg:(NSString*) msg{
BOOLisHaveDot =YES;
if([textfield.textrangeOfString:@"."].location==NSNotFound) {
isHaveDot =NO;
}
if(isHaveDot) {//存在小数点
NSRangerange = [textfield.textrangeOfString:@"."];
if(textfield.text.length- range.location> (dotBitCount +1)) {
[WAGToastUtil showToastInRootViewWithMsg:msg];
NSUIntegerlen = textfield.text.length;
textfield.text= [textfield.textsubstringToIndex:len -1];
}
if(range.location> intBitCount) {
[WAGToastUtil showToastInRootViewWithMsg:msg];
textfield.text= [textfield.textsubstringFromIndex:range.location- intBitCount];
}
}else{
NSUIntegerlen = textfield.text.length;
if(len > intBitCount) {
[WAGToastUtil showToastInRootViewWithMsg:msg];
textfield.text= [textfield.textsubstringToIndex:intBitCount];
}
}
}
调用:
[self handleInPut:textfield IntBitCount:6 dotBitCount:2 toastMsg:@"仅支持6位整数2位小数"];
判断是空字符串
- (BOOL)isBlankString:(NSString*)str {
NSString *noBlankStr = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
noBlankStr = [noBlankStrstringByReplacingOccurrencesOfString:@"\n" withString:@""];
noBlankStr = [noBlankStrstringByReplacingOccurrencesOfString:@"\r" withString:@""];
return(noBlankStr.length==0);
}
网友评论