I need to calculate the frame size for given text that may be in english/emoji/other languages.
The way I did it was:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize], NSParagraphStyleAttributeName: style};
CGSize rect = [text boundingRectWithSize:CGSizeMake(containerWidth, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading
attributes:attributes
context:nil].size;
It works fine as long as I do not use emoji. It seems that the emoji line height/font size is different than regular fonts.
I saw that people tried solving it in different ways such as: http://youbbe.xyz/issue/4987325/ios-emoji-messed-up-in-uilabel
This solution works for emojis but not for English/other fonts.
See example image:
As you can see, just text is calculated fine (first box) but text+emoji is not calculated correctly (second box).
I'm really surprised that there is no easy solution for this problem. Your help is much appreciated.
To summarize, you can calculate the height of a label by using its string and calling boundingRectWithSize . You must provide the font as an attribute, and include . usesLineFragmentOrigin for multi-line labels.
To give a dynamic height to an UIlabel in swift we can use the frame property of UILabel. We can create a frame using the CGRect which allows us to give different variables like x position, y position, width, and height.
With a UILabel you can simply say:
CGSize size = [label sizeThatFits:CGSizeMake(containerWidth, CGFLOAT_MAX)];
Have you tried this? I don't think emoji have a different line height, but rather they typically take up the entire line height.
You can just say [label sizeToFit];
and then get the size from the frame, (e.g. CGSize size = label.frame.size;
). This seems the most consistently accurate way of getting the right size for different font sizes.
There is some code here that you can copy and paste into a playground and see the different results.
A possible solution is set text with attribute string and specify paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 5;
paragraphStyle.lineHeightMultiple = 1.1;
paragraphStyle.maximumLineHeight = 16;
maximumLineHeight
can limit height of line which has emoji in it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With