textField textView输入限制

1、小数点后两位,只能输入数字和小数点
@property (nonatomic, assign) BOOL isHaveDian;
@property (nonatomic, assign) BOOL isFirstZero;
if ([textField.text rangeOfString:@"."].location==NSNotFound) {
_isHaveDian = NO;
}
if ([textField.text rangeOfString:@"0"].location==NSNotFound) {
_isFirstZero = NO;
}

if ([string length]>0)
{
    if (textField.text.length > 12) {
        return NO;
    }
    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.text isEqualToString:@"0.0"]){
                    return NO;
                }
                NSRange ran=[textField.text rangeOfString:@"."];
                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.text rangeOfString:@"."];
                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;
}

2、textView输入位数的限制

  • (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    {
    UITextRange *selectedRange = [textView markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    //获取高亮部分内容
    //NSString * selectedtext = [textView textInRange:selectedRange];

    //如果有高亮且当前字数开始位置小于最大限制时允许输入
    if (selectedRange && pos) {
    NSInteger startOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.start];
    NSInteger endOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.end];
    NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);

      if (offsetRange.location < maxNum) {
          return YES;
      }
      else
      {
          return NO;
      }
    

    }

NSString *comcatstr = [textView.text stringByReplacingCharactersInRange:range withString:text];

NSInteger caninputlen = maxNum - comcatstr.length;

if (caninputlen >= 0)
{
    return YES;
}
else
{
    NSInteger len = text.length + caninputlen;
    //防止当text.length + caninputlen < 0时,使得rg.length为一个非法最大正数出错
    NSRange rg = {0,MAX(len,0)};
    
    if (rg.length > 0)
    {
        NSString *s = @"";
        //判断是否只普通的字符或asc码(对于中文和表情返回NO)
        BOOL asc = [text canBeConvertedToEncoding:NSASCIIStringEncoding];
        if (asc) {
            s = [text substringWithRange:rg];//因为是ascii码直接取就可以了不会错
        }
        else
        {
            __block NSInteger idx = 0;
            __block NSString  *trimString = @"";//截取出的字串
            //使用字符串遍历,这个方法能准确知道每个emoji是占一个unicode还是两个
            [text enumerateSubstringsInRange:NSMakeRange(0, [text length])
                                     options:NSStringEnumerationByComposedCharacterSequences
                                  usingBlock: ^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop) {
                                      
                                      if (idx >= rg.length) {
                                          *stop = YES; //取出所需要就break,提高效率
                                          return ;
                                      }
                                      
                                      trimString = [trimString stringByAppendingString:substring];
                                      
                                      idx++;
                                  }];
            
            s = trimString;
        }
        //rang是指从当前光标处进行替换处理(注意如果执行此句后面返回的是YES会触发didchange事件)
        [textView setText:[textView.text stringByReplacingCharactersInRange:range withString:s]];
        //既然是超出部分截取了,哪一定是最大限制了。
        NSLog(@"%@",[NSString stringWithFormat:@"%d/%ld",0,(long)maxNum]);
    }
    return NO;
}

}

  • (void)textViewDidChange:(UITextView *)textView
    {
    UITextRange *selectedRange = [textView markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];

    //如果在变化中是高亮部分在变,就不要计算字符了
    if (selectedRange && pos) {
    return;
    }

    NSString *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;

    if (existTextNum > maxNum)
    {
    //截取到最大位置的字符(由于超出截部分在should时被处理了所在这里这了提高效率不再判断)
    NSString *s = [nsTextContent substringToIndex:maxNum];

      [textView setText:s];
    

    }

    NSLog(@"%@",[NSString stringWithFormat:@"%ld/%ld",MAX(0,maxNum - existTextNum),(long)maxNum]);
    }

你可能感兴趣的:(textField textView输入限制)