如何判断文字为一行or两行?
注意Vertical Overflow需要设置为Truncate
根据text.Preferred Height可判断文字为一行/两行,该属性会受到Font Style Size的影响
代码如下
public static void SetTextWithEllipsis(this Text textComponent, string value)
{
value = value.Replace(" ", "\u00A0");//解决空格问题
// create generator with value and current Rect
var generator = new TextGenerator();
var rectTransform = textComponent.GetComponent<RectTransform>();
var settings = textComponent.GetGenerationSettings(rectTransform.rect.size);
generator.Populate(value, settings);
// trncate visible value and add ellipsis
var characterCountVisible = generator.characterCountVisible;