From 9869b4e2f8d9b280e987034f58cedd96cd2f4600 Mon Sep 17 00:00:00 2001 From: Sean Dempsey Date: Tue, 7 Jun 2022 20:09:34 -0700 Subject: [PATCH] Use contentsizefitter for tight-fit text in buttons --- MagicUI/Elements/Button.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/MagicUI/Elements/Button.cs b/MagicUI/Elements/Button.cs index ff485d8..094115d 100644 --- a/MagicUI/Elements/Button.cs +++ b/MagicUI/Elements/Button.cs @@ -248,7 +248,10 @@ public Button(LayoutRoot onLayout, string name = "New Button") : base(onLayout, textComponent.fontSize = fontSize; textComponent.alignment = TextAnchor.MiddleCenter; textComponent.color = contentColor; - textTx.sizeDelta = MeasureText(); + + ContentSizeFitter fitter = textObj.AddComponent(); + fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; + fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; textObj.transform.SetParent(imgObj.transform, false); @@ -304,8 +307,6 @@ protected override void ArrangeOverride(Vector2 alignedTopLeftCorner) imgTx.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, ContentSize.x); imgTx.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, ContentSize.y); - textTx.sizeDelta = MeasureText(); - Vector2 pos = UI.UnityScreenPosition(alignedTopLeftCorner, ContentSize); imgTx.anchorMin = pos; imgTx.anchorMax = pos;