diff --git a/src/Controls/src/Core/Button/Button.iOS.cs b/src/Controls/src/Core/Button/Button.iOS.cs index d5776d32421a..6801980f2d34 100644 --- a/src/Controls/src/Core/Button/Button.iOS.cs +++ b/src/Controls/src/Core/Button/Button.iOS.cs @@ -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) @@ -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); + } } } diff --git a/src/Controls/src/Core/Platform/iOS/Extensions/ButtonExtensions.cs b/src/Controls/src/Core/Platform/iOS/Extensions/ButtonExtensions.cs index 0af36cf32a7e..802c7ea5b4b7 100644 --- a/src/Controls/src/Core/Platform/iOS/Extensions/ButtonExtensions.cs +++ b/src/Controls/src/Core/Platform/iOS/Extensions/ButtonExtensions.cs @@ -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 diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue21488.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue21488.cs new file mode 100644 index 000000000000..fb97fe3b4605 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue21488.cs @@ -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 + }; + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue21488.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue21488.cs new file mode 100644 index 000000000000..06a419c694e1 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue21488.cs @@ -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(); + } + } +} \ No newline at end of file