|
|
@@ -1,3 +1,4 @@
|
|
|
+using System;
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using UnityEngine;
|
|
|
@@ -6,19 +7,54 @@ using UnityEngine.UI;
|
|
|
public static class TextEllipsis
|
|
|
{
|
|
|
//超框显示
|
|
|
- public static void SetTextWithEllipsis(this Text textComponent, string value) {
|
|
|
+ //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;
|
|
|
+
|
|
|
+ //}
|
|
|
+ //加个判断文字charVisible
|
|
|
+ public static void SetTextWithEllipsis(this Text textComponent, string value)
|
|
|
+ {
|
|
|
+
|
|
|
+ var rect = textComponent.rectTransform.rect;
|
|
|
+ //Debug.Log($"[SetTextWithEllipsis] Text: {value}, Rect: {rect.width}x{rect.height}");
|
|
|
+
|
|
|
+ //if (rect.width <= 1f || rect.height <= 1f)
|
|
|
+ //{
|
|
|
+ // Debug.LogWarning("Text rect is too small or not initialized yet.");
|
|
|
+ //}
|
|
|
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)
|
|
|
+ var settings = textComponent.GetGenerationSettings(rect.size);
|
|
|
+ // 明确设置字体
|
|
|
+ settings.font = textComponent.font;
|
|
|
+ settings.fontSize = textComponent.fontSize;
|
|
|
+ settings.fontStyle = textComponent.fontStyle;
|
|
|
+ settings.scaleFactor = textComponent.canvas.scaleFactor;
|
|
|
+ settings.resizeTextForBestFit = false;
|
|
|
+ bool success = generator.Populate(value, settings);
|
|
|
+ //Debug.Log($"Populate success: {generator.Populate(value, settings)}, characterCountVisible: {generator.characterCountVisible}, textLength: {value.Length}");
|
|
|
+ //Debug.Log($"Font: {settings.font}, FontSize: {settings.fontSize}, Rect: {textComponent.rectTransform.rect.size}, ResizeText: {settings.resizeTextForBestFit}");
|
|
|
+ string updatedText = value;
|
|
|
+ var charVisible = generator.characterCountVisible;
|
|
|
+ if (value.Length > charVisible && charVisible > 0)
|
|
|
{
|
|
|
- updatedText = value.Substring(0, characterCountVisible);
|
|
|
- updatedText += "…";
|
|
|
+ int safeLength = Math.Max(0, charVisible - 1);
|
|
|
+ safeLength = Math.Min(safeLength, value.Length);
|
|
|
+ updatedText = value.Substring(0, safeLength) + "…";
|
|
|
}
|
|
|
- //Debug.Log(value + ",文本宽度为:" + generator.GetPreferredWidth(value, settings)+",width:"+ textComponent.rectTransform.rect.width);
|
|
|
textComponent.text = updatedText;
|
|
|
-
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|