Position uitextfieldFromPosition: offset does not work emojis

I am trying to implement my own keyboard containing emojis. To do this, I insert emoji at the cursor position.

This works great if 4-byte emoji characters do not exist in UITextField. Otherwise, the application crashes.

I post the insert code here. Can someone please indicate how to solve the problem?

UITextField *field = self.textField;
UITextRange *range = field.selectedTextRange;
int pos = [field offsetFromPosition:field.beginningOfDocument toPosition:range.end];
NSString * firstHalfString = [field.text substringToIndex:pos];  
NSString * secondHalfString = [field.text substringFromIndex:pos];  
field.text = [NSString stringWithFormat: @"%@%@%@", firstHalfString, emoticon, secondHalfString];
UITextPosition *newPos = [field positionFromPosition:field.beginningOfDocument offset:pos + 1];
field.selectedTextRange = [field textRangeFromPosition:newPos toPosition:newPos];

this line returns nil if there is emojis in the text:

UITextPosition *newPos = [field positionFromPosition:field.beginningOfDocument offset:pos + 1];
+5
source share
2 answers

In the end, I solved this by writing my own methods for calculating length and offset, which count 4-byte characters as 1 character, not two.

@implementation NSString (UnicodeAdditions)
-(NSInteger)utf32length {
    const char* bytes = [self UTF8String];
    int length = [self lengthOfBytesUsingEncoding:NSUTF16StringEncoding];
    int newLength = 0;
    for (int i=0; i<length; i++) {
        if (((unsigned char)bytes[i] >> 7) == 0b00000000)  {
          newLength++;
        }
        else if (((unsigned char)bytes[i] >> 5) == 0b00000110) {
            newLength++;
            i+=1;
        }
        else if (((unsigned char)bytes[i] >> 4) == 0b00001110) {
            newLength++;
            i+=2;
        }
        else if (((unsigned char)bytes[i] >> 3) == 0b00011110) {
            newLength++;
            i+=3;
        }
    }
    return newLength;
}


-(NSInteger)utf32offsetWithOffset:(NSInteger)offset {
    const char* bytes = [self UTF8String];
    int length = [self lengthOfBytesUsingEncoding:NSUTF16StringEncoding];
    int newLength = 0;
    for (int i=0; i<length && offset!=0; i++) {
        if (((unsigned char)bytes[i] >> 7) == 0b00000000)  {
            offset--;
            newLength++;
        }
        else if (((unsigned char)bytes[i] >> 5) == 0b00000110) {
            offset--;
            newLength++;
            i+=1;
        }
        else if (((unsigned char)bytes[i] >> 4) == 0b00001110) {
            offset--;
            newLength++;
            i+=2;
        }
        else if (((unsigned char)bytes[i] >> 3) == 0b00011110) {
            offset-=2;
            newLength++;
            i+=3;
        }
    }
    return newLength;
}

@end

see full blog post http://bit.ly/PT9VSz

+4
source

, :

- (NSRange)findRealRangeForString:(NSString *)text range:(NSRange)range
{
    __block int loc = 0;
    __block int len = 0;
    [text enumerateSubstringsInRange:NSMakeRange(0, text.length) options:(NSStringEnumerationByComposedCharacterSequences) usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
     {
         if (substringRange.location < range.location)
             loc++;
         else if (substringRange.location < range.location + range.length)
             len++;
         else
             *stop = YES; 
     }];
    return NSMakeRange(loc, len);
}
+2

All Articles