Skip to content

Commit

Permalink
Use contentsizefitter for tight-fit text in buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
BadMagic100 committed Jun 8, 2022
1 parent 84e44c8 commit 9869b4e
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions MagicUI/Elements/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContentSizeFitter>();
fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;

textObj.transform.SetParent(imgObj.transform, false);

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9869b4e

Please sign in to comment.