10

I am currently using

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode

to get the size of an NSString. However, when that string includes emojis, it seems to calculate the size for the literal unicode character rather than taking into account the size of the emoji itself, rendering the returned size incorrect.

How do I correctly get the size of the string with emoji characters, as it will appear in a uilabel?

Four
  • 406
  • 4
  • 13
  • 2
    Perhaps you could give a concrete (short) example of a string, the computed size and the expected size. – Martin R Apr 12 '13 at 07:57
  • 1
    I've just tried with a mix of different emoji and roman characters, different fonts, and the size is always correct... Maybe the problem is with your `constrainedToSize:` parameter, which is too small to contain the string... – Guillaume Apr 12 '13 at 08:55
  • 1
    By default all emoji are presented at a fixed size, regardless of font size and both NSString and UIFont will ignore the font size completely. I believe that emoji characters are always drawn at around 20px (non-retina) on iOS, so they will not measure correctly with the rest of the string. – Thomas Denney Apr 12 '13 at 16:19
  • There are actually two sizes (I think) for emoji, small and big. However, in my own tests, `NSString` always gave the correct size... – Guillaume Apr 12 '13 at 17:37

2 Answers2

2

The NSString is not presenting the emoji, it's representing a string, so the sizeWithFont will only account for the string.

I would use:

CGRect labelFrame = label.frame;  
labelFrame.size = [label sizeThatFits:CGSizeMake(100, 9999)];  
[label setFrame:labelFrame]; 

or

//Alternatively  
[label sizeToFit];

Bare in mind that sizeToFit calls the sizeThatFits: method, so in terms of just setting the label to the right height, sizeThatFits: is quicker, and much easier on the eye.

Monolo
  • 18,205
  • 17
  • 69
  • 103
user352891
  • 1,181
  • 1
  • 8
  • 14
  • 1
    I am sorry to disappoint, but you will find that this will not solve the problem, as noted in my stack here : http://stackoverflow.com/questions/19537354/emojis-messing-with-obj-cs-sizewithfont-math – Nils Munch Oct 23 '13 at 09:04
1

I struggled with this same thing for a while, attempting multiple solutions including the accepted answer, which did not work for me. I solved this by creating an NSAttributed String with the text, then using the NSAttributedString method boundingRectWithSize:options:context: to get the size of the string.

NSString *text = //some text
CGFloat maxSize = //text size constraints

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text
                                                                       attributes:@{NSFontAttributeName : font
                                                                                    }];

CGRect boundingRect = [attributedString boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGSize fitSize = boundingRect.size;
lramirez135
  • 2,872
  • 3
  • 18
  • 18