How to get NSString size when NSString includes emojis?

后端 未结 2 1818
醉话见心
醉话见心 2021-01-17 16:30

I am currently using

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

to

2条回答
  •  粉色の甜心
    2021-01-17 17:16

    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.

提交回复
热议问题