Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 57 additions & 17 deletions MenuLib/MonoBehaviors/REPOLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,61 @@ namespace MenuLib.MonoBehaviors;

public sealed class REPOLabel : REPOElement
{
public TextMeshProUGUI labelTMP;

private void Awake()
{
rectTransform = (RectTransform) transform;
labelTMP = GetComponentInChildren<TextMeshProUGUI>();

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<TextMeshProUGUI>();

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();
}
}
}