0

when I use boundingRectWithSize to calculate the height of the text,it crash.I find it maybe because the text have some emoji. Somebody can tell me how to solve this.

    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

iCloudy
  • 1
  • 2

1 Answers1

0

You first convert into Encoding string and then get height:

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;
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20