diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21837.xaml b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21837.xaml
new file mode 100644
index 000000000000..99a5703ba492
--- /dev/null
+++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21837.xaml
@@ -0,0 +1,93 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21837.xaml.cs b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21837.xaml.cs
new file mode 100644
index 000000000000..3c886a6d8271
--- /dev/null
+++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21837.xaml.cs
@@ -0,0 +1,29 @@
+using Microsoft.Maui.Controls;
+using Microsoft.Maui.Controls.Xaml;
+namespace Maui.Controls.Sample.Issues;
+
+[XamlCompilation(XamlCompilationOptions.Compile)]
+[Issue(IssueTracker.Github, 21837, "Span's TapGestureRecognizer not working if text is truncated", PlatformAffected.All)]
+
+public partial class Issue21837 : ContentPage
+{
+ private int _tapCount;
+ public int TapCount
+ {
+ get => _tapCount;
+ set
+ {
+ _tapCount = value;
+ OnPropertyChanged();
+ }
+ }
+
+ public Command TapCommand { get; set; }
+
+ public Issue21837()
+ {
+ InitializeComponent();
+ TapCommand = new Command(() => ++TapCount);
+ BindingContext = this;
+ }
+}
diff --git a/src/Controls/src/Core/Platform/iOS/Extensions/FormattedStringExtensions.cs b/src/Controls/src/Core/Platform/iOS/Extensions/FormattedStringExtensions.cs
index c9ee60dc7025..6253f557772a 100644
--- a/src/Controls/src/Core/Platform/iOS/Extensions/FormattedStringExtensions.cs
+++ b/src/Controls/src/Core/Platform/iOS/Extensions/FormattedStringExtensions.cs
@@ -184,7 +184,7 @@ internal static void RecalculateSpanPositions(this UILabel control, Label elemen
var yaxis = startRect.Top;
var lineHeights = new List();
- while ((endRect.Bottom - yaxis) > 0.001)
+ while (Math.Max(endRect.Bottom, finalSize.Bottom) - yaxis > 0.001)
{
double lineHeight;
if (yaxis == startRect.Top) // First Line
@@ -197,8 +197,14 @@ internal static void RecalculateSpanPositions(this UILabel control, Label elemen
}
else // Bottom Line
{
- lineHeight = endRect.Bottom - endRect.Top;
+ lineHeight = Math.Max(endRect.Bottom - endRect.Top, finalSize.Bottom - finalSize.Top);
}
+
+ if (lineHeight == 0)
+ {
+ break;
+ }
+
lineHeights.Add(lineHeight);
yaxis += (float)lineHeight;
}
@@ -211,7 +217,7 @@ internal static void RecalculateSpanPositions(this UILabel control, Label elemen
}
else
{
- ((ISpatialElement)span).Region = Region.FromLines(lineHeights.ToArray(), finalSize.Width, startRect.X, endRect.X, startRect.Top).Inflate(5);
+ ((ISpatialElement)span).Region = Region.FromLines(lineHeights.ToArray(), finalSize.Width, startRect.X, Math.Max(endRect.X, finalSize.Width - startRect.X), startRect.Top).Inflate(5);
}
// Update current location
diff --git a/src/Controls/tests/UITests/Tests/Issues/Issue21837.cs b/src/Controls/tests/UITests/Tests/Issues/Issue21837.cs
new file mode 100644
index 000000000000..dc55f58d5ac2
--- /dev/null
+++ b/src/Controls/tests/UITests/Tests/Issues/Issue21837.cs
@@ -0,0 +1,29 @@
+using NUnit.Framework;
+using UITest.Appium;
+using UITest.Core;
+
+namespace Microsoft.Maui.AppiumTests.Issues
+{
+ public class Issue21837 : _IssuesUITest
+ {
+ public Issue21837(TestDevice device) : base(device)
+ {
+ }
+
+ public override string Issue => "Span's TapGestureRecognizer not working if text is truncated";
+
+ [Test]
+ public void TapsShouldBeCorrectlyRecognized()
+ {
+ int numberOfTappableLabels = 7;
+
+ _ = App.WaitForElement("label1");
+
+ for (int i = 0; i < numberOfTappableLabels; i++)
+ App.Click($"label{i + 1}");
+
+ var resultText = App.FindElement("resultLabel").GetText();
+ Assert.AreEqual(resultText, $"Number of recognized taps: {numberOfTappableLabels}");
+ }
+ }
+}