Emojis messing with obj-c's sizeWithFont math

前端 未结 3 678
青春惊慌失措
青春惊慌失措 2021-02-01 07:10

In a UITableView that needs to display a long list of chatlike conversations, often containing emojis, a size calculation error occurs.

My problem is, that

3条回答
  •  清酒与你
    2021-02-01 07:36

    Thank you @SergiSolanellas! Here's a version that takes an attributedString, shortening the method because the text and font are already set.

    //
    // Given an attributed string that may have emoji characters and the width of 
    // the display area, return the required display height.
    //
    - (CGFloat)heightForAttributedStringWithEmojis:(NSAttributedString *)attributedString forWidth:(CGFloat)width {
        CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);
        CFRange fitRange;
        CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(width, CGFLOAT_MAX), &fitRange);
    
        CFRelease(framesetter);
    
        return frameSize.height;
    }
    

提交回复
热议问题