unity 自适应Text高度,多余文字省略号替代

	/// <summary>
    /// 换行自适应Text高度
    /// 多余文字 ... 代替
    /// </summary>
    /// <param name="value"></param>
    /// <param name="targetText"></param>
    /// <returns></returns>
    public static string SetTextWithEllipsis(string value, Text targetText)
    {
    
    
        TextGenerator generator = new TextGenerator();
        string textStr;
        var settings = targetText.GetGenerationSettings(targetText.gameObject.GetComponent<RectTransform>().rect.size);
        generator.Populate(value, settings);
        var characterCountVisible = generator.characterCountVisible;
        var updatedText = value;
        if (value.Length > characterCountVisible)
        {
    
    
            updatedText = value.Substring(0, characterCountVisible - 3);
            updatedText += "…";
        }
        textStr = updatedText;

        return textStr;
    }

猜你喜欢

转载自blog.csdn.net/weixin_47819574/article/details/129438747