UITableViewAutomaticDimension does not set the correct height with emoji characters in UILabel UITableviewCell

I am using UITableViewAutomaticDimension for the dynamic height of UITableview cells. Everything works fine when I set plain text in a shortcut inside a UITableviewCell.

The problem arises with cell height when I set plain text along with emoji characters in the label. The cell height increases dynamically, but the height is incorrect due to emoji characters. I think a UITableview can treat unicode as text instead of the emoji icon, so it only returns the height of the text.

In my case, the x origin shortcut is also dynamic.

Please see the screenshots below for the problem,

Any suggestions?

+4
source share
1 answer

Delete UITableViewAutomaticDimension, calculate the label height dynamically below the specified methods and accordingly control the cell height.

// --- Calculate the height of the line --- //

+(CGFloat)textHeight:(NSString*)text withFont:(UIFont*)font andMaxWidth:(CGFloat)maxWidth 
{
     CGFloat maxHeight = 99999;
     CGSize maximumLabelSize = CGSizeMake(maxWidth,maxHeight);
     NSDictionary *attributes = @{NSFontAttributeName: font};
     CGRect expectedLabelSize = [text boundingRectWithSize:maximumLabelSize options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil];
     return expectedLabelSize.size.height;
}
0
source

Source: https://habr.com/ru/post/1659704/


All Articles