diff --git a/MenuLib/MonoBehaviors/REPOLabel.cs b/MenuLib/MonoBehaviors/REPOLabel.cs index b6d4e43..288ba88 100644 --- a/MenuLib/MonoBehaviors/REPOLabel.cs +++ b/MenuLib/MonoBehaviors/REPOLabel.cs @@ -5,21 +5,61 @@ namespace MenuLib.MonoBehaviors; public sealed class REPOLabel : REPOElement { - public TextMeshProUGUI labelTMP; - - private void Awake() - { - rectTransform = (RectTransform) transform; - labelTMP = GetComponentInChildren(); - - labelTMP.rectTransform.pivot = rectTransform.pivot = Vector2.zero; - labelTMP.rectTransform.sizeDelta = rectTransform.sizeDelta = new Vector2(200f, 30f); - - labelTMP.fontSize = 30; - labelTMP.enableWordWrapping = labelTMP.enableAutoSizing = false; - labelTMP.alignment = TextAlignmentOptions.Left; - labelTMP.margin = Vector4.zero; - } - - private void Start() => labelTMP.rectTransform.localPosition = Vector2.zero; + public TextMeshProUGUI labelTMP; + public float maxFontSize = 30f; + public float minFontSize = 20f; + public bool autoScaleToFit = true; + + private void Awake() + { + rectTransform = (RectTransform)transform; + labelTMP = GetComponentInChildren(); + + labelTMP.rectTransform.pivot = rectTransform.pivot = Vector2.zero; + labelTMP.rectTransform.sizeDelta = rectTransform.sizeDelta = new Vector2(200f, 30f); + + labelTMP.enableAutoSizing = false; + labelTMP.fontSize = maxFontSize; + + labelTMP.enableWordWrapping = false; + labelTMP.alignment = TextAlignmentOptions.Left; + labelTMP.margin = Vector4.zero; + } + + private void Start() + { + labelTMP.rectTransform.localPosition = Vector2.zero; + + if (autoScaleToFit) + { + ScaleFontToFit(); + } + } + + public void ScaleFontToFit() + { + labelTMP.fontSize = maxFontSize; + labelTMP.ForceMeshUpdate(); + + var textBounds = labelTMP.textBounds; + var rectWidth = labelTMP.rectTransform.rect.width; + + if (textBounds.size.x > rectWidth) + { + float scaleFactor = rectWidth / textBounds.size.x; + float newSize = Mathf.Max(minFontSize, maxFontSize * scaleFactor * 0.95f); + labelTMP.fontSize = newSize; + labelTMP.ForceMeshUpdate(); + } + } + + public void SetText(string text, bool scaleToFit = true) + { + labelTMP.text = text; + + if (scaleToFit) + { + ScaleFontToFit(); + } + } } \ No newline at end of file