Skip to content

Commit

Permalink
fix: Handle justified text for TextBlock on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Aug 17, 2021
1 parent 89b65f5 commit 8104c69
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBlockControl\TextBlock_JustifiedText.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBlockControl\TextBlock_Layout.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -5395,6 +5399,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBlockControl\TextBlock_IsTextSelectionEnabled.xaml.cs">
<DependentUpon>TextBlock_IsTextSelectionEnabled.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBlockControl\TextBlock_JustifiedText.xaml.cs">
<DependentUpon>TextBlock_JustifiedText.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBlockControl\TextBlock_Layout.xaml.cs">
<DependentUpon>TextBlock_Layout.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Page
x:Class="UITests.Windows_UI_Xaml_Controls.TextBlockControl.TextBlock_JustifiedText"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Windows_UI_Xaml_Controls.TextBlockControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
<TextBlock Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
MaxLines="4"
TextWrapping="Wrap"
TextAlignment="Justify"
HorizontalAlignment="Left" />
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Uno.UI.Samples.Controls;
using Windows.UI.Xaml.Controls;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace UITests.Windows_UI_Xaml_Controls.TextBlockControl
{
[Sample("TextBlockControl", "TextBlock_JustifiedText")]
public sealed partial class TextBlock_JustifiedText : Page
{
public TextBlock_JustifiedText()
{
this.InitializeComponent();
}
}
}
14 changes: 13 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private static void InitializeStaticLayoutInterop()
private TextPaint _paint;
private TextUtils.TruncateAt _ellipsize;
private Android.Text.Layout.Alignment _layoutAlignment;
private JustificationMode _justificationMode;

/// <summary>
/// Used by unit tests to verify that the displayed color matches the nominal managed color.
Expand Down Expand Up @@ -175,15 +176,21 @@ private void UpdateLayoutAlignment()
{
case TextAlignment.Center:
_layoutAlignment = Android.Text.Layout.Alignment.AlignCenter;
_justificationMode = JustificationMode.None;
break;

case TextAlignment.Right:
_layoutAlignment = Android.Text.Layout.Alignment.AlignOpposite;
_justificationMode = JustificationMode.None;
break;
case TextAlignment.Justify:
_layoutAlignment = Android.Text.Layout.Alignment.AlignNormal;
_justificationMode = JustificationMode.InterWord;
break;

default:
case TextAlignment.Left:
_layoutAlignment = Android.Text.Layout.Alignment.AlignNormal;
_justificationMode = JustificationMode.None;
break;
}
}
Expand Down Expand Up @@ -404,6 +411,7 @@ private Size UpdateLayout(ref LayoutBuilder layout, Size availableSize, bool exa
_paint,
IsLayoutConstrainedByMaxLines ? TruncateEnd : _ellipsize, // .SetMaxLines() won't work on Android unless the ellipsize "END" is used.
_layoutAlignment,
_justificationMode,
TextWrapping,
MaxLines,
availableSize,
Expand Down Expand Up @@ -445,6 +453,7 @@ private class LayoutBuilder : IEquatable<LayoutBuilder>
private readonly Java.Lang.ICharSequence _textFormatted;
private readonly TextUtils.TruncateAt _ellipsize;
private readonly Android.Text.Layout.Alignment _layoutAlignment;
private readonly JustificationMode _justificationMode;
private readonly TextWrapping _textWrapping;
private readonly int _maxLines;
private readonly bool _exactWidth;
Expand Down Expand Up @@ -475,6 +484,7 @@ public LayoutBuilder(
TextPaint paint,
TextUtils.TruncateAt ellipsize,
Android.Text.Layout.Alignment layoutAlignment,
JustificationMode isJustifiedText,
TextWrapping textWrapping,
int maxLines,
Size availableSize,
Expand All @@ -488,6 +498,7 @@ LayoutBuilder existingBuilder
_paint = paint;
_ellipsize = ellipsize;
_layoutAlignment = layoutAlignment;
_justificationMode = isJustifiedText;
_textWrapping = textWrapping;
_maxLines = maxLines;
AvailableSize = availableSize;
Expand Down Expand Up @@ -720,6 +731,7 @@ private void MakeLayout(int width, int maxLines = int.MaxValue)
.SetEllipsizedWidth(width)
.SetAlignment(_layoutAlignment)
.SetIncludePad(true)
.SetJustificationMode(_justificationMode)
.Build();
}

Expand Down

0 comments on commit 8104c69

Please sign in to comment.