- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
功能:
把textField中位置为range的字符串替换为string字符串;
此函数在textField内容被修改时调用;
返回值:
YES,表示修改生效;NO,表示不做修改,textField的内容不变。
参数说明:
textField:响应UITextFieldDelegate协议的UITextField控件。
range: UITextField控件中光标选中的字符串,即被替换的字符串;
range.length为0时,表示在位置range.location插入string。
string: 替换字符串;
string.length为0时,表示删除。
}
一. 限制 textField输入最大长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"原字符:%@",textField.text);
NSLog(@"位置:%ld",range.location);
NSLog(@"长度:%ld",range.length);
NSLog(@"替换字符:%@",string);
//
if (textField == _textField) {
if (textField == self.textField) {
if (string.length == 0) return YES;
NSInteger existedLength = textField.text.length;
NSInteger selectedLength = range.length;
NSInteger replaceLength = string.length;
if (existedLength - selectedLength + replaceLength > 6) {
[textField resignFirstResponder];
return NO;
}
}
}
return YES;
}
二.最多输入两位 小数
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField == _moneyTextField) {
NSMutableString * futureString = [NSMutableString stringWithString:textField.text];
[futureString insertString:string atIndex:range.location];
NSInteger flag=0;
const NSInteger limited = 2;
for (NSInteger i = futureString.length-1; i>=0; i--) {
if ([futureString characterAtIndex:i] == '.') {
if (flag > limited) {
return NO;
}
break;
}
flag++;
}
}
return YES;
}
// 最完美的UITextField中输入金额,只能输入数字和小数点,保留两位小数点且0放在首位
@property (nonatomic, assign) BOOL isHaveDian;
@property (nonatomic, assign) BOOL isFirstZero;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == self.amountTextField) {
if ([textField.textrangeOfString:@"."].location==NSNotFound) {
_isHaveDian = NO;
}
if ([textField.textrangeOfString:@"0"].location==NSNotFound) {
_isFirstZero = NO;
}
if ([string length]>0)
{
unichar single=[string characterAtIndex:0];//当前输入的字符
if ((single >='0' && single<='9') || single=='.')//数据格式正确
{
if([textField.text length]==0){
if(single == '.'){
//首字母不能为小数点
return NO;
}
if (single == '0') {
_isFirstZero = YES;
return YES;
}
}
if (single=='.'){
if(!_isHaveDian)//text中还没有小数点
{
_isHaveDian=YES;
return YES;
}else{
return NO;
}
}else if(single=='0'){
if ((_isFirstZero&&_isHaveDian)||(!_isFirstZero&&_isHaveDian)) {
//首位有0有.(0.01)或首位没0有.(10200.00)可输入两位数的0
if([textField.textisEqualToString:@"0.0"]){
return NO;
}
NSRange ran=[textField.textrangeOfString:@"."];
int tt=(int)(range.location-ran.location);
if (tt <= 2){
return YES;
}else{
return NO;
}
}else if (_isFirstZero&&!_isHaveDian){
//首位有0没.不能再输入0
return NO;
}else{
return YES;
}
}else{
if (_isHaveDian){
//存在小数点,保留两位小数
NSRange ran=[textField.textrangeOfString:@"."];
int tt= (int)(range.location-ran.location);
if (tt <= 2){
return YES;
}else{
return NO;
}
}else if(_isFirstZero&&!_isHaveDian){
//首位有0没点
return NO;
}else{
return YES;
}
}
}else{
//输入的数据格式不正确
return NO;
}
}else{
return YES;
}
}
return YES;
}