textField输入限制位数(中文表情)


[self.tfNickName addTarget:self action:@selector(TFChange:) forControlEvents:UIControlEventEditingChanged];

- (void)TFChange:(UITextField *)textField{
    NSString *str = textField.text;
    NSInteger j = [self isChinese:str];
    if (j == 0) {
        DLog(@"没中文");
        if (str.length > 8 * 2) {
            textField.text = [str substringToIndex:8 * 2];
        }
    }else{
        DLog(@"有中文");
        if (str.length > 8) {
            textField.text = [str substringToIndex:8];
        }
    }
}

- (NSInteger)isChinese:(NSString *)str{
    int j = 0;
    for (int i = 0; i < str.length; i++) {
        NSInteger c = [str characterAtIndex:i];
        if (c>0x4e00 && c<0x9fff) {
            j++;
        }
    }
    return j;
}

3.13更
上边会出现表情被截取,汉字高亮部分过长无法输入的bug.修改如下:

- (void)TFChange:(UITextField *)textField{
    NSString *str = textField.text;
    NSInteger j = [self isChinese:str];
    DLog(@"输入的是%@",str);
    if (j == 0) {
        DLog(@"没中文");
        if (str.length > 10 * 2) {
            NSRange rangeIndex = [str rangeOfComposedCharacterSequenceAtIndex:10 * 2];
            
            //如果是汉字,就直接截取到限制的最大字符数
            if (rangeIndex.length == 1)
            {
                textField.text = [str substringToIndex:10 * 2];
            }
            
            //如果不是汉字,那就是emoji表情了,就截取到包括完整emoji表情后的range范围的字符
            else
            {
                NSRange rangeRange = [str rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 10 * 2)];
                textField.text = [str substringWithRange:rangeRange];
            }
           // textField.text = [str substringToIndex:10 * 2];
        }
    }else{
        DLog(@"有中文");
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        DLog(@"高亮%@,%@",selectedRange,position);
        if (!position) {
            if (str.length > 10) {
                //判断第三方中文输入法的emoji表情
                NSRange rangeIndex = [str rangeOfComposedCharacterSequenceAtIndex:10];
                if (rangeIndex.length == 1)
                {
                    textField.text = [str substringToIndex:10];
                }
                else
                {
                    NSRange rangeRange = [str rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 10)];
                    textField.text = [str substringWithRange:rangeRange];
                }
                
              /*  //中文和emoj表情存在问题,需要对此进行处理
                NSRange range;
                NSUInteger inputLength = 0;
                for(int i=0; i < str.length && inputLength <= 10; i += range.length) {
                    range = [textField.text rangeOfComposedCharacterSequenceAtIndex:i];
                    inputLength += [str substringWithRange:range].length;
                    if (inputLength > 10) {
                        NSString* newText = [str substringWithRange:NSMakeRange(0, range.location)];
                        textField.text = newText;
                    }
                }
               */
                
              //  textField.text = [str substringToIndex:10];
            }
        }
    }
}

参考文章http://www.jianshu.com/p/79d76051aecc.

你可能感兴趣的:(textField输入限制位数(中文表情))