NSString包含表情符号时如何获得NSString大小? [英] How to get NSString size when NSString includes emojis?

查看:209
本文介绍了NSString包含表情符号时如何获得NSString大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用

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

获取NSString的大小。但是,当该字符串包含表情符号时,它似乎计算文字unicode字符的大小,而不是考虑表情符号本身的大小,使得返回的大小不正确。

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.

如何使用表情符号正确获取字符串的大小,因为它将显示在uilabel中?

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

推荐答案

NSString没有呈现表情符号,它表示一个字符串,因此sizeWithFont只会考虑字符串。

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

我会使用:

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

//Alternatively  
[label sizeToFit];

请记住 sizeToFit 调用 sizeThatFits:方法,因此就将标签设置到正确的高度而言,sizeThatFits:更快,更容易看到。

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.

这篇关于NSString包含表情符号时如何获得NSString大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆