Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

attributed strings with emojis - proper height

I'm trying to get the height of an attributed string. This is working as expected with:

[attrString boundingRectWithSize:size
                         options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine)
                         context:NULL]

...unless the string contains emojis. With emojis, the string is cut off at the bottom of the label sized with this. Is there something special I need to do?

like image 476
livings124 Avatar asked Nov 01 '13 18:11

livings124


1 Answers

I know this is an old post but someone might still find it useful, you could drop to core text and let that do the hard work for you!

static inline CGSize OKASizeWithAttributedString(NSAttributedString *attributedString, CGFloat width) {

  CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributedString);
  CGSize targetSize = CGSizeMake(width, CGFLOAT_MAX);
  CGSize size = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, (CFIndex)[attributedString length]), NULL, targetSize, NULL);
  CFRelease(framesetter);

  return size;
}
like image 100
Oliver Atkinson Avatar answered Nov 14 '22 13:11

Oliver Atkinson