Bug 52541 - Label CenterAndExpand does not expand, yet StartAndExpand does
Summary: Label CenterAndExpand does not expand, yet StartAndExpand does
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.3
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jimmy [MSFT]
Depends on:
Reported: 2017-02-15 21:13 UTC by Clint
Modified: 2017-11-16 23:49 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

StartAndExpand v. CenterAndExpand (91.19 KB, image/jpeg)
2017-02-15 21:13 UTC, Clint

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Clint 2017-02-15 21:13:49 UTC
Created attachment 19832 [details]
StartAndExpand v. CenterAndExpand

Please see attached.
As the name implies "CenterAndExpand" should expand to fill available space.  It does not.  One would expect it to act exactly like "StartAndExpand" only to be centered.  It does not.

Changing nothing more than this one property yields two dramatically different renders.
Comment 1 Jimmy [MSFT] 2017-02-24 21:32:15 UTC
This appears to be expected results and not a bug. Within a StackLayout, a child will only expand in the orientation of the layout. The child will also not change the size of its view unless FillAndExpand is used[1.

In your screenshot, if the Label is inside a vertical StackLayout then the HorizontalOptions will be ignored and the centering in the first Label is coming from the HorizontalTextAlignment instead. I also could not reproduce the second Label shown in your screenshot unless I used FillAndExpand, not StartAndExpand as mentioned.

If you are seeing different results than specified in the documentation, please attach a reproduction project and re-open the report so we can look into this some more. Thanks!

[1] https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/layout-options/#Expansion
Comment 2 Clint 2017-02-24 21:52:47 UTC

> FillAndExpand, not StartAndExpand as mentioned.

Rather than be difficult and argue a small point...
From a user perspective 'expand' is still 'expand' whether it is start,end, or center.
If FillAndExpand causes the control to expand to available space then it seems 'CenterAndExpand' should do the same.

The placement of the control within the parent should not be tied to the HorizontalTextAlignment.

If I want the Label itself to be placed in the center, but have the text align at the Start - that's reasonable.
If I want the Label to be placed at the start, and have the text be centered in the Label, that's reasonable - but different.
Theoretically if you combined this with custom renderers you can get some pretty cool effects: Which is kinda the point of them.

Either way, if I want the label to Expand to fill available space the rendered output shouldn't be dramatically different.  Expand should be Expand; and you admit that you get different kinds of expansion depending on center or fill.

From the page you linked:
A StackLayout can't expand child views in the direction opposite to its orientation. Therefore, on a vertically oriented StackLayout, setting the HorizontalOptions property on a child view to StartAndExpand has the same effect as setting the property to Start.

This sounds like a bug: Possibly the core of this bug.  Why can't it?  If I have a Vertical stack of objects, I should absolutely be able to have each item within the stack expand horizontally differently from each other and in an unrelated relationship to the StackLayout itself.

Common use case example: A chat window such as the Messages app on your iPhone.
That's a StackLayout in Vertical orientation. Probably FillAndExpand.
The items (chat bubbles) commonly change position/expansion.  
If the bubble is from me then it is EndAndExpand while its text is HorizontalTextAlign=Start.
If the bubble is from the other person it is StartAndExpand, still with text alignment centered.
Comment 3 Jason Smith [MSFT] 2017-03-21 18:07:43 UTC
This is an API design flaw.

The expand flag should have been an attached property of the StackLayout instead of part of the LayoutOptions. The API would have then looked like:

  <Label HorizontalOptions="Center" StackLayout.IsExpander="true" />

This would make it clear the Expand flag is talking about how the StackLayout allocates leftover space, and the LayoutOptions talk about how that space is then used with Fill being the default. Unfortunately Im a dumbass and didn't design it right in the beginning.
Comment 4 Clint 2017-03-21 18:35:12 UTC
I wouldn't say you're a dumbarse.

I also wouldn't say it should be part of the StackLayout, per sae.

A label either can or cannot expand to fit available free space - and should have nothing to do with the specific type of container it is in.

If you have a picture and a label under it.
|              |
|              |
|              |
|              |
|              |

Maybe you want the label to just be centered.
|              |
|              |
|              |
|              |
|              |

Maybe you want the label to be centered, and expand to the width of the parent so its colored background is full width.
|              |
|              |
|              |
|              |
|              |

Whether its in a grid, StackLayout, ListView, CustomWrapLayout, ContentView... Shouldn't matter.

Or maybe you want to be StartAndExpand because the BackgroundColor is RED alerting the user to something important.
|              |
|              |
|              |
|              |
|              |
Comment 5 John Hardman 2017-11-16 23:49:07 UTC
I've just hit this again. Having read the previous comments, and a related forum thread, I have to say that the current situation is counter-intuitive. Even though the online documentation about expansion has been updated to describe (albeit vaguely in places) what is happening currently, and at the risk of a breaking change, I'd say both code and documentation should be modified to do what Clint has described above. Just my two-pennies worth.