| 123456789101112131415161718192021222324 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public static class TextEllipsis
- {
- //超框显示
- public static void SetTextWithEllipsis(this Text textComponent, string value) {
- TextGenerator generator = new TextGenerator();
- var settings = textComponent.GetGenerationSettings(textComponent.rectTransform.rect.size);
- generator.Populate(value, settings);
- var characterCountVisible = generator.characterCountVisible;
- var updatedText = value;
- if (value.Length > characterCountVisible)
- {
- updatedText = value.Substring(0, characterCountVisible);
- updatedText += "…";
- }
- //Debug.Log(value + ",文本宽度为:" + generator.GetPreferredWidth(value, settings)+",width:"+ textComponent.rectTransform.rect.width);
- textComponent.text = updatedText;
- }
- }
|