我正在尝试实现我自己的包含表情符号的键盘。 为此,我将表情符号插入光标位置。
如果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];
答案 0 :(得分: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
答案 1 :(得分: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);
}