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

Commit

Permalink
Fix RTL on Labels with FormattedText and/or LineHeight (#13197) fixes #…
Browse files Browse the repository at this point in the history
…3311 fixes #12473 fixes #8157

* Add Issue3311.cs

* Update Issue331.cs test file

* Fix RTL issue on FormattedText

* Add relevant RTL FormattedText test

* Update Issue3311.cs

* Add Issue12473

* Add BackgroundColor to test labels for clarit

* Add LabelTest for RTL Label with LineHeight

* Add missing closing EmbeddedResource tag

* Remove UITest template code

* Update RTL tests to be more thorough

* UpdateHorizontalTextAlignment on UpdateFormattedText

Fix RTL issue that arises when certain text properties are set on a Label w/ RTL and FormattedText (i.e. FontAttributes, FontSize, LineHeight, TextColor, TextTransform, etc)

* Remove TextType from tests

TextType on FormattedText labels causes app crash. This separate issue will be fixed in a follow-up PR
  • Loading branch information
rachelkang committed Jan 27, 2021
1 parent 42a3faa commit 1304996
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<controls:TestContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:Xamarin.Forms.Controls"
x:Class="Xamarin.Forms.Controls.Issues.Issue12473">
<ScrollView Margin="20">
<StackLayout>
<Label Text="This test passes if all proceeding labels are properly right-aligned" FontSize="20" HorizontalTextAlignment="Center" />

<Label Text="This is a normal text label with RTL" FlowDirection="RightToLeft" BackgroundColor="Red"/>
<Label Text="This is a normal text label with RTL and LineHeight" FlowDirection="RightToLeft" LineHeight="3" BackgroundColor="Yellow"/>

<!--Test the same thing with Labels in an RTL language-->
<Label Text="هذا نص حشو عشوائي أتمنى ترجمته بشكل صحيح من الإنجليزية" FlowDirection="RightToLeft" BackgroundColor="Red"/>
<Label Text="هذا نص حشو عشوائي أتمنى ترجمته بشكل صحيح من الإنجليزية" FlowDirection="RightToLeft" LineHeight="3" BackgroundColor="Yellow"/>

<!--Test the same thing with Labels using both LTR and RTL languages-->
<Label Text="foo هذا نص حشو عشوائي أتمنى ترجمته بشكل صحيح من الإنجليزية" FlowDirection="RightToLeft" BackgroundColor="Red"/>
<Label Text="foo هذا نص حشو عشوائي أتمنى ترجمته بشكل صحيح من الإنجليزية" FlowDirection="RightToLeft" LineHeight="3" BackgroundColor="Yellow"/>
</StackLayout>
</ScrollView>
</controls:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Xaml;

#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
#if APP
[XamlCompilation(XamlCompilationOptions.Compile)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 12473, "iOS Label text alignment in RTL with LineHeight", PlatformAffected.iOS)]
public partial class Issue12473 : TestContentPage
{
public Issue12473()
{
#if APP
InitializeComponent();
#endif
}

protected override void Init()
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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.Github, 3311, "RTL is not working for iOS Label with FormattedText", PlatformAffected.Default)]
public class Issue3311 : TestContentPage
{
protected override void Init()
{
var formattedString = new FormattedString();
formattedString.Spans.Add(new Span { Text = "RTL formatted text" });

Content = new StackLayout()
{
Margin = 20,

Children =
{
new Label()
{
AutomationId = "Issue3311Label",
Text = "This test passes if all proceeding labels are properly right-aligned",
HorizontalTextAlignment = TextAlignment.Center,
FontSize = 20
},
new Label()
{
AutomationId = "Issue3311NormalTextLabel",
Text = "RTL normal text",
FlowDirection = FlowDirection.RightToLeft,

BackgroundColor = Color.Red,
HeightRequest = 100,
LineBreakMode = LineBreakMode.WordWrap,
Margin = 20,
MaxLines = 1,
Opacity = 50,
Padding = 5,
TextDecorations = TextDecorations.Underline,
VerticalTextAlignment = TextAlignment.Center,
FontAttributes = FontAttributes.Bold,
FontSize = 20,
LineHeight = 3,
TextColor = Color.Blue,
TextTransform = TextTransform.Uppercase,
TextType = TextType.Html,
HorizontalTextAlignment = TextAlignment.Start
},
new Label()
{
AutomationId = "Issue3311FormattedTextLabel",
FormattedText = formattedString,
FlowDirection = FlowDirection.RightToLeft,

BackgroundColor = Color.Yellow,
HeightRequest = 100,
LineBreakMode = LineBreakMode.WordWrap,
Margin = 20,
MaxLines = 1,
Opacity = 50,
Padding = 5,
TextDecorations = TextDecorations.Underline,
VerticalTextAlignment = TextAlignment.Center,
FontAttributes = FontAttributes.Bold,
FontSize = 20,
LineHeight = 3,
TextColor = Color.Blue,
TextTransform = TextTransform.Uppercase,
HorizontalTextAlignment = TextAlignment.Start
}
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,11 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue8988.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12084.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12512.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3311.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12473.xaml.cs">
<DependentUpon>Issue12473.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue12809.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11969.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11381.xaml.cs" />
Expand Down Expand Up @@ -2076,6 +2081,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue12084.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue12473.xaml">
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue12809.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down
76 changes: 76 additions & 0 deletions Xamarin.Forms.Platform.iOS.UnitTests/LabelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Threading.Tasks;
using NUnit.Framework;
using UIKit;

namespace Xamarin.Forms.Platform.iOS.UnitTests
{
[TestFixture]
public class LabelTests : PlatformTestFixture
{
[Test, Category("Label"), Category("RTL"), Category("FormattedText")]
[Description("RTL should work on Label with FormattedText")]
public async Task RTLWorksOnLabelWithFormattedText()
{
var formattedString = new FormattedString();
formattedString.Spans.Add(new Span { Text = "Label with RTL and FormattedText" });
var label = new Label { FormattedText = formattedString };
label.FlowDirection = FlowDirection.RightToLeft;
var expected = UITextAlignment.Right;
var actual = await GetControlProperty(label, uiLabel =>
{
label.BackgroundColor = Color.Yellow;
label.HeightRequest = 50;
label.LineBreakMode = LineBreakMode.WordWrap;
label.Margin = 20;
label.MaxLines = 1;
label.Opacity = 50;
label.Padding = 5;
label.TextDecorations = TextDecorations.Underline;
label.FontAttributes = FontAttributes.Bold;
label.FontSize = 20;
label.LineHeight = 3;
label.TextColor = Color.Blue;
label.TextTransform = TextTransform.Uppercase;
label.HorizontalTextAlignment = TextAlignment.Start;
label.VerticalTextAlignment = TextAlignment.Center;
return uiLabel.TextAlignment;
});
Assert.That(actual, Is.EqualTo(expected));
}

[Test, Category("Label"), Category("RTL"), Category("LineHeight")]
[Description("RTL should work on Label with LineHeight")]
public async Task RTLWorksOnLabelWithLineHeight()
{
var label = new Label { Text = "Label with RTL and LineHeight" };
label.FlowDirection = FlowDirection.RightToLeft;
label.LineHeight = 3;
var expected = UITextAlignment.Right;
var actual = await GetControlProperty(label, uiLabel =>
{
label.BackgroundColor = Color.Yellow;
label.HeightRequest = 50;
label.LineBreakMode = LineBreakMode.WordWrap;
label.Margin = 20;
label.MaxLines = 1;
label.Opacity = 50;
label.Padding = 5;
label.TextDecorations = TextDecorations.Underline;
label.FontAttributes = FontAttributes.Bold;
label.FontSize = 20;
label.LineHeight = 3;
label.TextColor = Color.Blue;
label.TextTransform = TextTransform.Uppercase;
label.TextType = TextType.Html;
label.HorizontalTextAlignment = TextAlignment.Start;
label.VerticalTextAlignment = TextAlignment.Center;
return uiLabel.TextAlignment;
});
Assert.That(actual, Is.EqualTo(expected));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="BackgroundTests.cs" />
<Compile Include="TimePickerTests.cs" />
<Compile Include="TransformationTests.cs" />
<Compile Include="LabelTests.cs" />
<Compile Include="ShapeTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
}

UpdateLineBreakMode();
UpdateHorizontalTextAlignment();
UpdateText();
UpdateTextDecorations();
UpdateTextColor();
UpdateFont();
UpdateMaxLines();
UpdateCharacterSpacing();
UpdatePadding();
UpdateHorizontalTextAlignment();
}

base.OnElementChanged(e);
Expand Down Expand Up @@ -466,6 +466,8 @@ void UpdateFormattedText()
Control.AttributedStringValue = _formatted.ToAttributed(Element, Element.TextColor, Element.HorizontalTextAlignment, Element.LineHeight);
#endif
_perfectSizeValid = false;

UpdateHorizontalTextAlignment();
}

void UpdateTextHtml()
Expand Down

0 comments on commit 1304996

Please sign in to comment.