When iOS can only input 15 characters, the last one is damaged because it belongs to the emoticon and accounts for two characters.


NSInteger length;


-(void)textFiledEditChanged:(NSNotification*)obj{

    UITextField *textField = (UITextField *)obj.object;

    

    NSString *toBeString = textField.text;

    

    DSLog(@"    ----toBeString-----     %@",toBeString);

    

    

    NSInteger length = [ self getStringLengthWithString : toBeString];

    

    

    DSLog ( @"-------- character length -------%ld" ,( long )length);

    

    NSString *lang = [[ UITextInputMode currentInputMode ] primaryLanguage ]; // Keyboard input mode

    

    if ([lang isEqualToString : @"zh-Hans" ]) { // Simplified Chinese input, including simplified pinyin, Jianti Wubi, simplified handwriting

        UITextRange *selectedRange = [textField markedTextRange];

        // Get the highlighted part

        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];

        //If there is no highlighted word, the word count and limit will be performed on the entered text

        if(!position) {

            if(toBeString.length > kHZMaxLength) {

                textField.text = [toBeString substringToIndex:kHZMaxLength];

                DSLog ( @"When   Chinese characters exceed   %@" , textField.text);

            }

        }

        //If there is a highlighted string, the text will not be counted and restricted temporarily

        else{

            

        }

    }

    //You can directly limit statistics other than the Chinese input method, regardless of other languages

    else{

        if(toBeString.length > kEGMaxLength) {

            

            textField.text= [toBeString substringToIndex:kEGMaxLength];


            DSLog(@"   其他超过字符的时候    %@",textField.text);


        }

    }

    

    

    //获取高亮部分

    UITextRange *selectedRange = [textField markedTextRange];

    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];

    

    // 没有高亮选择的字,则对已输入的文字进行字数统计和限制

    if (!position)

    {

        if (toBeString.length > 15 && textField.markedTextRange == nil)

        {

            //用字符串的字符编码指定索引查找位置     一次遍历一个子串, 而不是遍历一个unichar字符长度设置15

            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:14];

            if (rangeIndex.length == 1)

            {

                textField.text = [toBeString substringToIndex:15];

                DSLog(@"======  1  ====%@",textField.text);


            }

            else

            {

                //用字符串的字符编码指定区域段查找位置

                //                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, Max_Length)];

                textField.text = [toBeString substringWithRange:NSMakeRange(0, kEGMaxLength + 1)];

                DSLog(@"======  2  ====%@",textField.text);


            }

        } else {

            length = toBeString.length;

            

            DSLog(@"-----  length ----%ld",(long)length);

        }

    }

}



- (NSInteger)getStringLengthWithString:(NSString *)string

{

    __block NSInteger stringLength = 0;

    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])

                               options:NSStringEnumerationByComposedCharacterSequences

                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)

    {

        const unichar hs = [substring characterAtIndex:0];

        if (0xd800 <= hs && hs <= 0xdbff)

        {

            if (substring.length > 1)

            {

                const unichar ls = [substring characterAtIndex:1];

                const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;

                if (0x1d000 <= uc && uc <= 0x1f77f)

                {

                    stringLength += 1;

                }

                else

                {

                    stringLength += 1;

                }

            }

            else

            {

                stringLength += 1;

            }

        } else if (substring.length > 1)

        {

            const unichar ls = [substring characterAtIndex:1];

            if (ls == 0x20e3)

            {

                stringLength += 1;

            }

            else

            {

                stringLength += 1;

            }

        } else {

            if (0x2100 <= hs && hs <= 0x27ff)

            {

                stringLength += 1;

            }

            else if (0x2B05 <= hs && hs <= 0x2b07)

            {

                stringLength += 1;

            }

            else if (0x2934 <= hs && hs <= 0x2935)

            {

                stringLength += 1;

            }

            else if (0x3297 <= hs && hs <= 0x3299)

            {

                stringLength += 1;

            }

            else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50)

            {

                stringLength += 1;

            }

            else

            {

                stringLength += 1;

            }

        }

    }];

    return stringLength;

}



Guess you like

Origin blog.csdn.net/qq_27247497/article/details/51159748