Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Fix formatted text text type crash #13532

Merged
merged 7 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.ManualReview)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.None, 0, "Label with FormattedText, HTML, and Padding shouldn't cause crash", PlatformAffected.iOS)]
public class LabelFormattedTextHtmlPadding : TestContentPage
{
protected override void Init()
{
var formattedString = new FormattedString();
formattedString.Spans.Add(new Span { Text = "This test passes if app doesn't crash. Label is not expected to actually work" });

Content = new StackLayout()
{
Margin = 20,

Children =
{
new Label()
{
AutomationId = "LabelFormattedTextHtmlPaddingTest",
Text = "If you can see this text, this test has passed"
},
new Label()
{
AutomationId = "LabelFormattedTextHtmlPadding",
FormattedText = formattedString,
TextType = TextType.Html,
Padding = 5
}
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutBackground.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContentOffest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContentWithZeroMargin.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LabelFormattedTextHtmlPadding.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13436.xaml.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
25 changes: 25 additions & 0 deletions Xamarin.Forms.Platform.iOS.UnitTests/HtmlLabelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,30 @@ public async Task LabelFontDefaultDefersToHtml()

Assert.That(actualFont.PointSize, Is.EqualTo(expectedFontSize));
}

[Test, Category("Label"), Category("FormattedText")]
[Description("If Label has FormattedText, HTML, and Padding, app should not crash")]
public async Task LabelWithFormattedTextHTMLAndPaddingDoesNotCrashApp()
{
var formattedString = new FormattedString();
formattedString.Spans.Add(new Span { Text = "Label with FormattedText, HTML, and Padding" });
var label = new Label
{
FormattedText = formattedString,
TextType = TextType.Html,
Padding = 5
};

var expected = TextType.Html;
var actual = await GetRendererProperty(label, renderer =>
{
var uiLabel = (UILabel)(renderer as LabelRenderer).Control;
uiLabel.Frame = new CoreGraphics.CGRect(0, 0, 200, 200);
uiLabel.RecalculateSpanPositions(label);
return label.TextType;
}, true);

Assert.That(actual, Is.EqualTo(expected));
}
}
}
6 changes: 6 additions & 0 deletions Xamarin.Forms.Platform.iOS/Extensions/LabelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ internal static class LabelExtensions
{
public static void RecalculateSpanPositions(this NativeLabel control, Label element)
{
if (element == null)
return;

if (element.TextType == TextType.Html)
return;

if (element?.FormattedText?.Spans == null
|| element.FormattedText.Spans.Count == 0)
return;
Expand Down