5

我正在尝试实现我自己的包含表情符号的键盘。为此,我将表情符号插入光标位置。

如果 UITextField 中不存在 4 字节的表情符号字符,则此方法可以正常工作。否则应用程序会崩溃。

我在这里发布插入代码。有人可以指出如何解决这个问题吗?

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];

如果文本中有表情符号,则此行返回 nil:

UITextPosition *newPos = [field positionFromPosition:field.beginningOfDocument offset:pos + 1];
4

2 回答 2

4

最后,我通过编写自己的长度和偏移量计算方法解决了这个问题,将 4 字节字符计为 1 个字符,而不是 2 个。

@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

查看完整的博客文章http://bit.ly/PT9VSz

于 2012-10-02T03:02:56.210 回答
2

我找到了另一种方法,它对我有用:

- (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);
}
于 2014-01-11T10:49:16.843 回答