Skip to content

Commit

Permalink
Character spacing in button - fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaflo committed Nov 3, 2024
1 parent e8c96c5 commit 0779aff
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/Controls/src/Core/Button/Button.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@ private static void MapPadding(IButtonHandler handler, Button button)
public static void MapText(IButtonHandler handler, Button button)
{
handler.PlatformView?.UpdateText(button);

// Any text update requires that we update any attributed string formatting
MapFormatting(handler, button);
}

internal static void MapBorderWidth(IButtonHandler handler, Button button)
Expand All @@ -477,5 +480,11 @@ private protected override void OnHandlerChangingCore(HandlerChangingEventArgs a
_originalImageSize = CGSize.Empty;
_originalCGImage = null;
}

private static void MapFormatting(IButtonHandler handler, IText button)
{
// Update all of the attributed text formatting properties
handler.PlatformView?.UpdateCharacterSpacing(button);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,7 @@ public static void UpdateText(this UIButton platformButton, Button button)
{
var text = TextTransformUtilites.GetTransformedText(button.Text, button.TextTransform);
platformButton.SetTitle(text, UIControlState.Normal);

// The TitleLabel retains its previous text value even after a new value is assigned. As a result, the label does not display the updated text and reverts to the old value when the button is re-measured
if (string.IsNullOrEmpty(button.Text))
{
platformButton.TitleLabel.Text = string.Empty;
}
platformButton.TitleLabel.Text = button.Text;

// Content layout depends on whether or not the text is empty; changing the text means
// we may need to update the content layout
Expand Down
27 changes: 27 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue21488.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Maui.Controls.Sample.Issues;

[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, "21488", "Button text doesn't update when CharacterSpacing is applied", PlatformAffected.iOS)]
public partial class Issue21488 : ContentPage
{
public Issue21488()
{
var buttonWithoutCharacterSpacing = new Button();
var buttonWithCharacterSpacing = new Button() { CharacterSpacing = 15 };
var entry = new Entry() { AutomationId = "Entry" };
entry.TextChanged += (s, e) =>
{
buttonWithoutCharacterSpacing.Text = entry.Text;
buttonWithCharacterSpacing.Text = entry.Text;
};

Content = new VerticalStackLayout()
{
new Label() { Text = "Button without character spacing:" },
buttonWithoutCharacterSpacing,
new Label() { Text = "Button with character spacing:" },
buttonWithCharacterSpacing,
entry
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue21488 : _IssuesUITest
{
public Issue21488(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "Button text doesn't update when CharacterSpacing is applied";

[Test]
[Category(UITestCategories.Button)]
public void ButtonTextShouldUpdate()
{
App.WaitForElement("Entry");
App.EnterText("Entry", "Hello, Maui!");
VerifyScreenshot();
}
}
}

0 comments on commit 0779aff

Please sign in to comment.