boundingRectWithSize与表情符号崩溃

时间:2017-02-28 04:53:53

标签: ios crash emoji

当我使用boundingRectWithSize来计算文本的高度时,它会崩溃。我发现它可能是因为文本有一些表情符号。 有人可以告诉我如何解决这个问题。

    CGFloat kScreenMargin = 15.;
    CGFloat kScreenWidth = [UIScreen mainScreen].bounds.size.width;

    self.content = @"Eefggg‍‍";

    CGFloat contentH = [self.content boundingRectWithSize:CGSizeMake(kScreenWidth - 2 * kScreenMargin, MAXFLOAT)
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height;

Here is the crash screenshot

1 个答案:

答案 0 :(得分:0)

首先转换为编码字符串然后获取高度:

CGFloat kScreenMargin = 15.;
    CGFloat kScreenWidth = [UIScreen mainScreen].bounds.size.width;
NSString *strEmo = @"Eefggg‍‍"
NSData *data = [strEmo dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
self.content = goodValue;
CGFloat contentH = [self.content boundingRectWithSize:CGSizeMake(kScreenWidth - 2 * kScreenMargin, MAXFLOAT)
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height;