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

Commit

Permalink
[Win] Labels will now wrap when inside horizontally infinite layouts (#…
Browse files Browse the repository at this point in the history
…639)

* Add repro for 42559

* [Win] Override GetDesiredSize for LabelRenderer

* [Win] Invalidate size on font/align change
  • Loading branch information
samhouts authored and rmarinho committed Mar 14, 2017
1 parent e7f5a41 commit 05f0f76
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
@@ -0,0 +1,67 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Linq;
using System;

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Bugzilla, 42599, "LineBreakMode does not work on UWP", PlatformAffected.WinRT)]
public class Bugzilla42599 : TestContentPage
{
protected override void Init()
{
var scrollView = new ScrollView();
var layout = new StackLayout();

foreach (var lineBreakMode in Enum.GetValues(typeof(LineBreakMode)).Cast<LineBreakMode>())
{
layout.Children.Add(GetLayout(lineBreakMode));
}
scrollView.Content = layout;
Content = scrollView;
}

static StackLayout GetLayout(LineBreakMode lineBreakMode)
{
var text = "";

switch (lineBreakMode)
{
default:
case LineBreakMode.NoWrap:
text = "This is a long sentence that should NOT wrap. If this sentence has wrapped, then this test has failed.";
break;
case LineBreakMode.WordWrap:
text = "This is a long sentence that should word wrap. If this sentence has NOT wrapped, then this test has failed.";
break;
case LineBreakMode.CharacterWrap:
text = "This is a long sentence that should character wrap. If this sentence has NOT wrapped, then this test has failed.";
break;
case LineBreakMode.HeadTruncation:
text = "This is a long sentence that should truncate at the beginning. If this sentence has NOT truncated, then this test has failed.";
break;
case LineBreakMode.TailTruncation:
text = "This is a long sentence that should truncate at the end. If this sentence has NOT truncated, then this test has failed.";
break;
case LineBreakMode.MiddleTruncation:
text = "This is a long sentence that should truncate at the middle. If this sentence has NOT truncated, then this test has failed.";
break;
}

var label = new Label
{
LineBreakMode = lineBreakMode,
Text = text,
};

var layout = new StackLayout
{
Children = { label },
Orientation = StackOrientation.Horizontal
};

return layout;
}
}
}
Expand Up @@ -251,6 +251,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla28650.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla37431.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla44777.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla42599.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla51503.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla51505.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla52533.cs" />
Expand Down
35 changes: 35 additions & 0 deletions Xamarin.Forms.Platform.WinRT/LabelRenderer.cs
Expand Up @@ -35,6 +35,8 @@ public class LabelRenderer : ViewRenderer<Label, TextBlock>
{
bool _fontApplied;
bool _isInitiallyDefault;
SizeRequest _perfectSize;
bool _perfectSizeValid;

protected override Windows.Foundation.Size ArrangeOverride(Windows.Foundation.Size finalSize)
{
Expand All @@ -61,6 +63,29 @@ protected override Windows.Foundation.Size ArrangeOverride(Windows.Foundation.Si
return finalSize;
}

public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
{
if (!_perfectSizeValid)
{
_perfectSize = base.GetDesiredSize(double.PositiveInfinity, double.PositiveInfinity);
_perfectSize.Minimum = new Size(Math.Min(10, _perfectSize.Request.Width), _perfectSize.Request.Height);
_perfectSizeValid = true;
}

if (widthConstraint >= _perfectSize.Request.Width && heightConstraint >= _perfectSize.Request.Height)
return _perfectSize;

var result = base.GetDesiredSize(widthConstraint, heightConstraint);
result.Minimum = new Size(Math.Min(10, result.Request.Width), result.Request.Height);
if (Element.LineBreakMode != LineBreakMode.NoWrap)
{
if (result.Request.Width > widthConstraint || Element.LineBreakMode == LineBreakMode.WordWrap || Element.LineBreakMode == LineBreakMode.CharacterWrap)
result.Request = new Size(Math.Max(result.Minimum.Width, widthConstraint), result.Request.Height);
}

return result;
}

protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
Expand Down Expand Up @@ -100,6 +125,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE

void UpdateAlign(TextBlock textBlock)
{
_perfectSizeValid = false;

if (textBlock == null)
return;

Expand Down Expand Up @@ -129,6 +156,8 @@ void UpdateColor(TextBlock textBlock)

void UpdateFont(TextBlock textBlock)
{
_perfectSizeValid = false;

if (textBlock == null)
return;

Expand All @@ -146,6 +175,8 @@ void UpdateFont(TextBlock textBlock)

void UpdateLineBreakMode(TextBlock textBlock)
{
_perfectSizeValid = false;

if (textBlock == null)
return;

Expand All @@ -164,6 +195,7 @@ void UpdateLineBreakMode(TextBlock textBlock)
textBlock.TextWrapping = TextWrapping.Wrap;
break;
case LineBreakMode.HeadTruncation:
// TODO: This truncates at the end.
textBlock.TextTrimming = TextTrimming.WordEllipsis;
textBlock.TextWrapping = TextWrapping.NoWrap;
break;
Expand All @@ -172,6 +204,7 @@ void UpdateLineBreakMode(TextBlock textBlock)
textBlock.TextWrapping = TextWrapping.NoWrap;
break;
case LineBreakMode.MiddleTruncation:
// TODO: This truncates at the end.
textBlock.TextTrimming = TextTrimming.WordEllipsis;
textBlock.TextWrapping = TextWrapping.NoWrap;
break;
Expand All @@ -182,6 +215,8 @@ void UpdateLineBreakMode(TextBlock textBlock)

void UpdateText(TextBlock textBlock)
{
_perfectSizeValid = false;

if (textBlock == null)
return;

Expand Down

0 comments on commit 05f0f76

Please sign in to comment.