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

Fix RTL on Labels with FormattedText and/or LineHeight #13197

Merged
merged 16 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,77 @@
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 \nformatted \ntext" });

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",
FlowDirection = FlowDirection.RightToLeft,
BackgroundColor = Color.Red,
Text = "RTL normal text"
},
new Label()
{
AutomationId = "Issue3311FormattedTextLabel",
FlowDirection = FlowDirection.RightToLeft,
BackgroundColor = Color.Yellow,
FormattedText = formattedString
},
new Label()
{
AutomationId = "Issue3311FormattedTextWithLineHeightLabel",
FlowDirection = FlowDirection.RightToLeft,
BackgroundColor = Color.Red,
LineHeight = 3,
FormattedText = formattedString
}

}
};
}

#if UITEST
[Test]
public void Issue1Test()
rachelkang marked this conversation as resolved.
Show resolved Hide resolved
{
RunningApp.WaitForElement("Issue1Label");
// Delete this and all other UITEST sections if there is no way to automate the test. Otherwise, be sure to rename the test and update the Category attribute on the class. Note that you can add multiple categories.
RunningApp.Screenshot("I am at Issue1");
RunningApp.WaitForElement(q => q.Marked("Issue1Label"));
RunningApp.Screenshot("I see the Label");
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,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 @@ -2052,6 +2057,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue12084.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue12473.xaml">
<SubType>Designer</SubType>
rachelkang marked this conversation as resolved.
Show resolved Hide resolved
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue12809.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down
35 changes: 35 additions & 0 deletions Xamarin.Forms.Platform.iOS.UnitTests/LabelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Threading.Tasks;
using NUnit.Framework;
using UIKit;

namespace Xamarin.Forms.Platform.iOS.UnitTests
{
[TestFixture]
public class LabelTests : PlatformTestFixture
{
[Test, Category("RTL"), Category("Label")]
[Description("RTL should work on Label with FormattedText")]
public async Task RTLLabelWithFormattedTextWorks()
{
var formattedString = new FormattedString();
formattedString.Spans.Add(new Span { Text = "RTL formatted text" });
var label = new Label { FormattedText = formattedString };
label.FlowDirection = FlowDirection.RightToLeft;
var expected = UITextAlignment.Right;
var actual = await GetControlProperty(label, uiLabel => uiLabel.TextAlignment);
Assert.That(actual, Is.EqualTo(expected));
}

[Test, Category("RTL"), Category("Label")]
[Description("RTL should work on Label with LineHeight")]
public async Task RTLLabelWithLineHeightWorks()
{
var label = new Label { Text = "RTL text with LineHeight" };
label.FlowDirection = FlowDirection.RightToLeft;
label.LineHeight = 3;
var expected = UITextAlignment.Right;
var actual = await GetControlProperty(label, uiLabel => uiLabel.TextAlignment);
Assert.That(actual, Is.EqualTo(expected));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="TextTests.cs" />
<Compile Include="BackgroundTests.cs" />
<Compile Include="TransformationTests.cs" />
<Compile Include="LabelTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Xamarin.Forms.Core\Xamarin.Forms.Core.csproj">
Expand Down
2 changes: 1 addition & 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