用emojis归因字符串 - 适当的高度

liv*_*124 7 nsstring nsattributedstring uilabel ios emoji

我正在尝试获取属性字符串的高度.这正如预期的那样:

[attrString boundingRectWithSize:size
                         options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine)
                         context:NULL]
Run Code Online (Sandbox Code Playgroud)

...除非字符串包含表情符号.使用表情符号时,字符串会在标签底部被切掉.我需要做些什么吗?

Oli*_*son 3

我知道这是一篇旧文章,但有人可能仍然觉得它有用,您可以转到核心文本,让它为您完成艰苦的工作!

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;
}
Run Code Online (Sandbox Code Playgroud)